在Android中使用SQLite未将数据插入表中

时间:2015-03-08 06:03:35

标签: java android sqlite android-activity android-sqlite

我想在包含4列id,名称,编号,地址的表格中插入供应商名称,供应商联系电话,供应商地址

不幸的是,每次执行都会抛出异常。尝试通过代码,logcat。仍然无法找到问题。任何形式的帮助都会非常感激。

Logcat信息:

03-08 10:46:07.920  28315-28315/com.example.bharathduraiswamy.comboedittext E/SQLiteDatabase﹕ Error inserting supplier address= supplier name= supplier mobile number=
android.database.sqlite.SQLiteException: near "address": syntax error (code 1): , while compiling: INSERT INTO SUPPLIERLIST(supplier address,supplier name,supplier mobile number) VALUES (?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
        at com.example.bharathduraiswamy.comboedittext.VivzDatabaseAdapter.insertSupplier(VivzDatabaseAdapter.java:92)
        at com.example.bharathduraiswamy.comboedittext.AddSupplier.addSupplier(AddSupplier.java:269)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(SupportMenuInflater.java:255)
        at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
        at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
        at android.view.View.performClick(View.java:4213)
        at android.view.View$PerformClick.run(View.java:17448)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5336)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

助手类代码:

    public long insertSupplier(String suppName, String suppNumber, String suppAddress) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(VivzHelper.COLUMN_SUPPLIER_NAME, suppName);
    contentValues.put(VivzHelper.COLUMN_SUPPLIER_MOBILE_NUMBER, suppNumber);
    contentValues.put(VivzHelper.COLUMN_SUPPLIER_ADDRESS, suppAddress);
    long supplier_id = db.insert(VivzHelper.TABLE_NAME_SUPPLIER, null, contentValues);
    db.close();
    return supplier_id;
}

供应商清单的表格数据:

        private static final String TABLE_NAME_SUPPLIER = "SUPPLIERLIST";
    private static final String COLUMN_SUPPLIER_UID = "supplier_id";
    private static final String COLUMN_SUPPLIER_NAME = "supplier name";
    private static final String COLUMN_SUPPLIER_MOBILE_NUMBER = "supplier mobile number";
    private static final String COLUMN_SUPPLIER_ADDRESS = "supplier address";
    private static final String CREATE_TABLE_SUPPLIER =
            "CREATE TABLE " + TABLE_NAME_SUPPLIER + "( " + COLUMN_SUPPLIER_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    " " + COLUMN_SUPPLIER_NAME + " VARCHAR(255)," +
                    " " + COLUMN_SUPPLIER_MOBILE_NUMBER + " VARCHAR(255)," +
                    " " + COLUMN_SUPPLIER_ADDRESS + " VARCHAR(255));";

    private static final String DROP_TABLE_SUPPLIER = "DROP TABLE IF EXISTS" + TABLE_NAME_SUPPLIER;

MainActivity.java中的addSupplier方法

   public void addSupplier(MenuItem item) {
    String suppName = supplierName.getText().toString();
    String suppNumber = supplierNumber.getText().toString();
    String suppAddress = supplierAddress.getText().toString();

    long id = vivzHelper.insertSupplier(suppName,suppNumber,suppAddress);
    if (id < 0) {
        Message.message(this, "Inserting a Row was unsuccessful");
    } else {
        Message.message(this, "Successfully inserted a Row");
    }
    //category.setText(""); clears the EditText's existing user fed data
    supplierName.setText("");
    supplierNumber.setText("");
    supplierAddress.setText("");
}

我正在使用操作栏中的菜单项将数据从三个EditText传递到SQLite数据库。

2 个答案:

答案 0 :(得分:1)

只需将列名更改为:

 private static final String COLUMN_SUPPLIER_NAME = "supplier_name";
private static final String COLUMN_SUPPLIER_MOBILE_NUMBER = "supplier_mobile_number";
private static final String COLUMN_SUPPLIER_ADDRESS = "supplier_address";

您在列名之间使用了空格。那是你的问题。

答案 1 :(得分:0)

我认为你的变量没有正确定义

private static final String TABLE_NAME_SUPPLIER = "SUPPLIERLIST";
private static final String COLUMN_SUPPLIER_UID = "supplier_id";
private static final String COLUMN_SUPPLIER_NAME = "supplier_name";
private static final String COLUMN_SUPPLIER_MOBILE_NUMBER = "supplier_mobile_number";
private static final String COLUMN_SUPPLIER_ADDRESS = "supplier_address";
private static final String CREATE_TABLE_SUPPLIER =
        "CREATE TABLE " + TABLE_NAME_SUPPLIER + "( " +COLUMN_SUPPLIER_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                " " + COLUMN_SUPPLIER_NAME + " VARCHAR(255)," +
                " " + COLUMN_SUPPLIER_MOBILE_NUMBER + " VARCHAR(255)," +
                " " + COLUMN_SUPPLIER_ADDRESS + " VARCHAR(255));";

用于插入使用吹出示例代码

public void addSupplier (Supplier supplier)
{
            //for logging
    Log.d("add", supplier.toString());

     // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(COLUMN_SUPPLIER_NAME, supplier.getSUPPLIER_NAME()); 
    values.put(COLUMN_SUPPLIER_ADDRESS, supplier.getSUPPLIER_ADDRESS());

            values.put(COLUMN_SUPPLIER_MOBILE_NUMBER, supplier.getSUPPLIER_MOBILE_NUMBER());

    // 3. insert
    db.insert(TABLE_NAME_SUPPLIER, // table
            null, //nullColumnHack
            values); // key/value -> keys = column names/ values = column values

    // 4. close
    db.close();
}