在Android

时间:2015-06-24 08:58:13

标签: android sqlite

我是Android编程的新手 我正在尝试将一些数据添加到SqLite数据库中 我在数据库中添加了一个名称和一个数字,但是我收到了语法错误 我检查了很多网站,但我几乎没有发现任何语法错误 如果有人能给予任何帮助,我将感激不尽。

这是我的代码:

@Override
    public void onCreate(SQLiteDatabase db) 
{
        String tablecreate = "create table " + tableName + " ( " + ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " TEXT,"
                + NUMBER + " TEXT )";

        db.execSQL(tablecreate);
    }

public void addContact(String name, String num) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(NAME, name);
        cv.put(NUMBER, num);
        database.insert(tableName, null, cv);
        database.close();
    }

这就是日志猫所说的:

06-24 15:09:04.930: E/SQLiteLog(1426): (1) near "number": syntax error
06-24 15:09:04.980: E/SQLiteDatabase(1426): Error inserting phone number=1234567890 name=mom
06-24 15:09:04.980: E/SQLiteDatabase(1426): android.database.sqlite.SQLiteException: near "number": syntax error (code 1): , while compiling: INSERT INTO CONTACTS_TABLE(phone number,name) VALUES (?,?)
06-24 15:09:04.980: E/SQLiteDatabase(1426):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-24 15:09:04.980: E/SQLiteDatabase(1426):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
06-24 15:09:04.980: E/SQLiteDatabase(1426):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
06-24 15:09:04.980: E/SQLiteDatabase(1426):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-24 15:09:04.980: E/SQLiteDatabase(1426):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)

1 个答案:

答案 0 :(得分:1)

您需要用方括号括住phone number 像这样[phone number]

在您的CREATE TABLE中以及您引用此字段的任何位置执行此操作。

或者,如果您始终使用名为NUMBER的字符串变量,请在变量定义中执行一次:

String NUMBER = "[phone number]";