java - Android SQL databse Error while creating -


i've made e- mail client android phone, , want save e- mails in sqlite database. if want fill database test data, error:

e/sqlitelog(21212): (1) near "to": syntax error

e/sqlitedatabase(21212): error inserting subject=test subject to=emailadress2 from=emailadress1 content=test content

android.database.sqlite.sqliteexception: near "to": syntax error (code 1): , while compiling: insert mail(subject,to,from,content) values (?,?,?,?)

(...)

(1) near "from": syntax error

e/error(21212): android.database.sqlite.sqliteexception: near "from": syntax error (code 1): , while compiling: select id, from, to, subject, content mail id = -1

i use following code database:

mainactivity.java

public void dbcreate(view view){     string = "emailadress1";     string = "emailadress2";     string subject = "test subject";     string content = "test content";      try {         datasource.open();         datasource.createentry(from, to, subject, content);         datasource.close();         }         catch (exception ex) {             toast.maketext(this,"error", toast.length_long).show();             log.e("error", ex.tostring());             } } 

mysqlitehelper.java

private static final string database_name = "mail.db"; private static final int database_version = 1;   private static final string table_create_mail = "create table mails (  id integer primary key autoincrement, text, text, subject text, content text);";    public mysqlitehelper(context context) {     super(context, database_name, null, database_version);  }  @override public void oncreate(sqlitedatabase database) {     log.i("info", "oncreate aufgerufen");     database.execsql(table_create_mail);  } 

datasource.java

private sqlitedatabase database; private mysqlitehelper dbhelper; private string[] allcolumns = { "id", "from",         "to", "subject", "content"}; public entry createentry(string from, string to, string subject, string content) {     contentvalues values = new contentvalues();     values.put("from", from);     values.put("to", to);     values.put("subject", subject);     values.put("content", content);      long insertid = database.insert("mail", null,             values);       cursor cursor = database.query("mail",allcolumns, "id = " + insertid, null, null, null, null, null);     cursor.movetofirst();      return cursortoentry(cursor); } 

to , from reserved word sqllite

please try:

insert mail(subject,`to`,`from`,content) values (?,?,?,?) 

if don't want execute arbitrary sql command, rename columns to_ , from_.

you try quote them like:

... private string[] allcolumns = { "id", "`from`", "`to`", "subject", "content"}; ... cv.put("`from`", from); cv.put("`to`", to); ... 

additionally: commented vinoth, table name must corrected (mail or mails)


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -