java - Caused by: android.database.sqlite.SQLiteException: near "": syntax error (code 1): , while compiling: -
11-27 03:32:04.471: e/androidruntime(23137): caused by: android.database.sqlite.sqliteexception: near "order": syntax error (code 1): , while compiling: create table order (_id integer primary key autoincrement, origin text not null, quantity integer not null);
mydatabase class:
public class mydatabase extends sqliteopenhelper { public static final string table_name = "order"; public static final string table_id = "_id"; public static final string table_origin = "origin"; public static final string table_quantity = "quantity"; private static final string database_name = "appple.db"; private static final int database_version = 1; private static final string database_create = "create table " + table_name + "(" + table_id + " integer primary key autoincrement, " + table_origin + " text not null, " + table_quantity + " integer not null);"; public mydatabase (context context) { super(context, database_name, null, database_version); } public void oncreate(sqlitedatabase db) { db.execsql(database_create); } public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } }
operation class:
public class operation { private mydatabase dbhelper; private string[] columns = { mydatabase.table_id, mydatabase.table_origin, mydatabase.table_quantity }; private sqlitedatabase database; public operation(context context) { dbhelper = new mydatabase(context); } public void open() throws sqlexception { database = dbhelper.getwritabledatabase(); } public void close() { dbhelper.close(); } public void add(string origin, string quantity) { contentvalues values = new contentvalues(); values.put(mydatabase.table_origin, origin); values.put(mydatabase.table_quantity, integer.parseint(quantity)); database.insert(mydatabase.table_name, null, values); } public int get(string origin) { int total = 0; cursor cursor = database.query(mydatabase.table_name, columns, mydatabase.table_origin + " = " + origin, null, null, null, null); cursor.movetofirst(); while(!cursor.isafterlast()) { total += cursor.getint(2); cursor.movetonext(); } cursor.close(); return total; } }
in mainactivity start with
operation op; op = new operation(this); op.open();
i think there no problem in create table. can't find cause of error.
order
keyword in sql. either rename table, or put table name in double quotes "order"
.
Comments
Post a Comment