将两个或多个数据插入数据库

时间:2016-10-17 17:59:16

标签: android android-sqlite

我想将两个或多个数据插入数据库,但它不起作用。这是我的Databasehelper课程:

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String CreateTABLE="CREATE TABLE IF NOT EXISTS " +
            TABLE_NAME + " (" +
            COL1_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL2_Barcode + " TEXT" +
            COL3_Time + " TEXT)";
    sqLiteDatabase.execSQL(CreateTABLE);
}

public boolean insertData(String barcodecontent, String systemtime) {
    db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2_Barcode, barcodecontent);
    contentValues.put(COL3_Time, systemtime);
    long result = db.insert(TABLE_NAME,null, contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

这是我的IncomingActivity课程:

public void AddData() {
    boolean isInserted = myDB.insertData(incoming_brc_textview.getText().toString(), incoming_hide_time.getText().toString());
    if (isInserted == true) {
        Toast.makeText(IncomingActivity.this, incoming_brc_textview.getText().toString() + "  store success", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(IncomingActivity.this, incoming_brc_textview.getText().toString() + "  store failure", Toast.LENGTH_LONG).show();
    }
}

有关如何解决此问题的任何建议?

1 个答案:

答案 0 :(得分:1)

将您的CreateTable查询更改为:

 String CreateTABLE="CREATE TABLE IF NOT EXISTS " +
        TABLE_NAME + " (" +
        COL1_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COL2_Barcode + " TEXT, " +
        COL3_Time + " TEXT)";

您的查询中似乎忘记了逗号。