1

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

E/SQLiteLog(21212): (1) "TO" 附近: 语法错误

E/SQLiteDatabase(21212): 插入错误 SUBJECT=test subject TO=emailadress2 FROM=emailadress1 CONTENT=test content

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

(...)

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

E/错误(21212):android.database.sqlite.SQLiteException:靠近“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);

}

数据源.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);
}
4

3 回答 3

2

TO并且FROMSQLLite 的保留字

请试试:

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

如果您不想执行任意 SQL 命令,您可以将列重命名为TO_and FROM_

您可以尝试像这样引用它们:

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

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

于 2013-05-04T11:31:02.203 回答
0

FROM并且TOkeywordsSQL,因此您应该更改列名。

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

看到这个

于 2013-05-04T11:29:14.510 回答
0

创建数据库时没有自动增量。在数据库中插入数据时尝试此代码

 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);
}
于 2013-05-04T11:35:46.680 回答