android sqlite数据没有添加

时间:2014-09-09 16:08:30

标签: android sqlite

我希望在android中向数据库添加一些数据。这是我的代码

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_ADD = "inventory";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_QUANTITY = "quantity";
  public static final String COLUMN_PRICE_PER_UNIT="price_per_unit";
  public static final String COLUMN_TIMESTAMP="added_at";
  public static final String COLUMN_ADD_TYPE="add_type";

  private static final String DATABASE_NAME = "inventory.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_ADD + "(" + COLUMN_ID
      + " text primary key, " + COLUMN_QUANTITY
      + " integer not null, "+COLUMN_PRICE_PER_UNIT
      +" integer not null, "+COLUMN_TIMESTAMP+" text, "+
      COLUMN_ADD_TYPE+" text not null );";

  public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(db_create);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ADD);
    onCreate(db);
  }

} 

更新 在将我的表名编辑为库存后,Bellow是我的日志,并且按照答案中的建议将列时间戳添加到added_at:

09-09 12:37:58.289: E/SQLiteLog(1423): (1) no such table: inventory
09-09 12:37:58.489: D/dalvikvm(1423): GC_FOR_ALLOC freed 396K, 12% free 3541K/4012K, paused 100ms, total 108ms
09-09 12:37:58.519: E/SQLiteDatabase(1423): Error inserting timestamp=2014-09-09 12:37:57 add_type=new entry price_per_unit=90 quantity=9 _id=Product
09-09 12:37:58.519: E/SQLiteDatabase(1423): android.database.sqlite.SQLiteException: no such table: inventory (code 1): , while compiling: INSERT INTO inventory(timestamp,add_type,price_per_unit,quantity,_id) VALUES (?,?,?,?,?)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at com.example.invent2.DataSource.insertValues(DataSource.java:43)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at com.example.invent2.AddActivity$2.onClick(AddActivity.java:53)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.view.View.performClick(View.java:4424)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.view.View$PerformClick.run(View.java:18383)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.os.Handler.handleCallback(Handler.java:733)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.os.Looper.loop(Looper.java:137)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at android.app.ActivityThread.main(ActivityThread.java:4998)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at java.lang.reflect.Method.invoke(Method.java:515)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
09-09 12:37:58.519: E/SQLiteDatabase(1423):     at dalvik.system.NativeStart.main(Native Method)

它说没有这样的表库存。

2 个答案:

答案 0 :(得分:1)

问题是,addSQLite Keyword。您的查询中必须escape it(插入选择):

INSERT INTO "add" (timestamp,add_type,price_per_unit,quantity,_id) VALUES (?,?,?,?,?)

通常,在逻辑内容之后命名表和列是个好主意,而不是它们的数据类型。我会将您的timestamp列重命名为added_at。表名应该包含有关正在添加的内容的更多信息。例如AddArticle

答案 1 :(得分:0)

将表名从ADD更改为其他名称。 SQL标准指定了大量关键字,这些关键字不能用作表,索引,列,数据库,用户定义函数,排序规则,虚拟表模块或任何其他命名对象的名称。 ADD就是这样一个关键字。参考 here