Android SQL数据库创建时出错

时间:2013-05-04 11:20:57

标签: java android mysql email

我为我的Android手机制作了一个电子邮件客户端,现在我想将电子邮件保存在SQLite数据库中。但是,如果我只想用测试数据填充数据库,我就是这个错误:

  

E / SQLiteLog(21212):( 1)靠近“TO”:语法错误

     

E / SQLiteDatabase(21212):插入SUBJECT =测试主题时出错TO = emailadress2 FROM = emailadress1 CONTENT =测试内容

     

android.database.sqlite.SQLiteException:靠近“TO”:语法错误(代码1):,同时编译:INSERT INTO MAIL(SUBJECT,TO,FROM,CONTENT)VALUES(?,?,?,?)< / p>      

(...)

     

(1)“FROM”附近:语法错误

     

E /错误(21212):android.database.sqlite.SQLiteException:near“FROM”:语法错误(代码1):,同时编译:SELECT ID,FROM,TO,SUBJECT,CONTENT from MAIL WHERE ID = - 1

我对数据库使用以下代码:

MAINACTIVITY.java

public void dbCreate(View view){
    String from = "emailadress1";
    String to = "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, FROM TEXT, TO 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);
}

3 个答案:

答案 0 :(得分:2)

TOFROMreserved word for SQLLite

请尝试:

INSERT INTO MAIL(SUBJECT,`TO`,`FROM`,CONTENT) VALUES (?,?,?,?)

如果您不想执行任意SQL命令,只需将列重命名为TO_FROM_

您可以尝试引用它们:

...
private String[] allColumns = { "ID", "`FROM`", "`TO`", "SUBJECT", "CONTENT"};
...
cv.put("`FROM`", from);
cv.put("`TO`", to);
...

此外:正如vinoth所评论,您的表名必须更正(MAIL或MAILS)

答案 1 :(得分:0)

FROM中{p> TOkeywordsSQL,因此您应更改列名。

SQL标准指定了大量关键字,这些关键字不能用作表,索引,列,数据库,用户定义函数,排序规则,虚拟表模块或任何其他命名对象的名称

See this

答案 2 :(得分:0)

创建DataBAse时没有autoIncrement .. 并在dataBase

中插入数据时尝试此代码
 public long creatEntry(Srting id, String from,
    String to,String subject, String content) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues(); //ContentValues kima el bundle
cv.put("ID", id);
cv.put("FROM", from);
    cv.put("TO", to);
    cv.put("SUBJECT", subject);
    cv.put("CONTENT", content);

return ourDatabase.insert(DATABASE_TABLE, null, cv);
}