java - Android SQL Database ERROR (Code 1) -
i'm trying display data database 1 one changing if next button clicked. , wanted check if program select first data database error message.
caused by: android.database.sqlite.sqliteexception: no such column: color (code 1): , while compiling: select distinct _id, brand, model, price, color, img car _id=1
dbadapter.java
import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; /** * created asus on 24.04.2015. */ public class dbadapter {private databasehelper dbhelper; private sqlitedatabase db; private final context context; public static final string key_rowid = "_id"; public static final string key_brand = "brand"; public static final string key_model = "model"; public static final string key_price = "price"; public static final string key_color = "color"; public static final string key_photo = "img"; private static final string tag = "dbadapter"; private static final string database_name = "testdb"; private static final string database_table = "car"; private static final int database_version = 1; private static final string database_create = "create table car (_id integer primary key autoincrement, " + "brand text, model text, price integer" + "color text, img blob);"; // constructor public dbadapter(context context) { this.context = context; dbhelper = new databasehelper(context); } // create , upgrade database in android application sqliteopenhelper subclass created private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { // oncreate() called framework, if database not exist log.d("create", "creating database"); try { db.execsql(database_create); } catch (sqlexception e) { e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // onupgrade() called framework, if 1 changes database version number // sends warn log message log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); // method execute sql statement directly db.execsql("drop table if exists contacts"); oncreate(db); } } // opens database public dbadapter open() throws sqlexception { // create and/or open database used reading , writing //db = dbhelper.getwritabledatabase(); // use if want read data database db = dbhelper.getreadabledatabase(); return this; } // closes database public void close() { // closes database dbhelper.close(); } // insert contact database public long insertcontact(int rowid, string brand, string model, int price, string color, byte [] photo) { // class contentvalues allows define key/values. "key" represents // table column identifier , "value" represents content table // record in column. contentvalues can used inserts , updates of database entries. contentvalues initialvalues = new contentvalues(); initialvalues.put(key_rowid, rowid); initialvalues.put(key_brand, brand); initialvalues.put(key_model, model); initialvalues.put(key_price, price); initialvalues.put(key_color, color); initialvalues.put(key_photo, photo); return db.insert(database_table, null, initialvalues); } // retrieves particular contact public cursor getcontact(int id) throws sqlexception { // rawquery() directly accepts sql select statement input. // query() provides structured interface specifying sql query. // query returns cursor object. cursor represents result of query // , points 1 row of query result. way android can buffer // query results efficiently; not have load data memory cursor mcursor = db.query(true, database_table, new string[] { key_rowid,key_brand, key_model, key_price, key_color, key_photo}, key_rowid + "=" + id, null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } }
displayactivity.java
import android.app.activity; import android.database.cursor; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.color; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.window; import android.widget.edittext; import android.widget.imageview; import android.widget.tablelayout; import android.widget.tablerow; import android.widget.textview; import android.widget.toast; import java.io.bytearrayoutputstream; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; public class displayactivity extends activity { private string [] car_images; private textview carid; private textview brand; private textview model; private textview price; private textview color; private imageview photo; private dbadapter db; private bitmap mbitmap; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.display); car_images = getresources().getstringarray(r.array.car_images); carid = (textview) findviewbyid(r.id.cariddb); brand = (textview) findviewbyid(r.id.branddb); model = (textview) findviewbyid(r.id.modeldb); price = (textview) findviewbyid(r.id.pricedb); color = (textview) findviewbyid(r.id.colordb); photo = (imageview) findviewbyid(r.id.photodb); // database created using dbadapter db = new dbadapter(this); // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.mito); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); db.open(); long id = db.insertcontact(1, "alfa romeo", "mito", 2900, "red",image); // long id = db.insertcontact("larry", "page", 3000000, image); // long id = db.insertcontact("steve", "ballmer", 5000000, image); cursor c = db.getcontact(1); if (c.movetofirst()) displaycontact(c); else toast.maketext(this, "no record found", toast.length_long) .show(); db.close(); } public void onclick(view view) { // contact db.open(); cursor c = db.getcontact(integer.parseint(carid.gettext().tostring())); if (c.movetofirst()) displaycontact(c); else toast.maketext(this, "no record found", toast.length_long) .show(); db.close(); } public void displaycontact(cursor c) { carid.settext(c.getstring(0)); brand.settext(c.getstring(1)); model.settext(c.getstring(2)); price.settext(c.getstring(3)); color.settext(c.getstring(4)); if (integer.parseint(c.getstring(0))==0){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.mito); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==1){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.giulettaa); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==2){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.alfa4c); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==3){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.gran); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==4){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.coupe); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==5){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.c3); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==6){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.c4); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==7){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.gla); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==8){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.smart); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else if (integer.parseint(c.getstring(0))==9){ // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.node); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } else { // add contact // bitmap must converted byte array can stored in blob mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.juke); // mbitmap = bitmapfactory.decoderesource(getresources(),r.drawable.google); bytearrayoutputstream mbytearrayoutputstream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.jpeg, 100, mbytearrayoutputstream); byte[] image = mbytearrayoutputstream.tobytearray(); image = c.getblob(5); bitmap mbitmap = bitmapfactory.decodebytearray(image, 0, image.length); photo.setimagebitmap(mbitmap); } } }
your problem here
private static final string database_create = "create table car (_id integer primary key autoincrement, " + "brand text, model text, price integer" // <-- no comma after 'integer'! + "color text, img blob);";
you missing comma. should be:
private static final string database_create = "create table car (_id integer primary key autoincrement, " + "brand text, model text, price integer," // <-- comma + "color text, img blob);";
Comments
Post a Comment