创建数据库时无法插入到android sqlite数据库中

时间:2015-03-24 11:14:52

标签: java android mysql database sqlite

我创建了一个SQLite数据库,我想在其中插入一些初始数据。所以我把插入"放入查询"在" onCreate"方法。 但它不会在数据库中插入任何值,并且在创建后为空!

这是我的代码:

package com.example.db;

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

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_GAME_LIST = "steps";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_STATUS = "status";
  public static final String COLUMN_HELP_COUNT = "helpCount";
  public static final String COLUMN_EAREASED = "earased";
  public static final String COLUMN_SPEED = "speed";

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

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_GAME_LIST + "(" + COLUMN_ID
      + " integer, " 
      + COLUMN_STATUS
      +" integer, "
      + COLUMN_HELP_COUNT
      +" integer, "
      + COLUMN_EAREASED
      +" string, "
      + COLUMN_SPEED
      +" integer"
      + " );";


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

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


      database.execSQL("INSERT INTO "+TABLE_GAME_LIST+"("+COLUMN_ID + ","
                + COLUMN_STATUS + ","
                        + COLUMN_HELP_COUNT + ","
                                + COLUMN_EAREASED + ","
                                        + COLUMN_SPEED + ") VALUES(14, 1, 2, "+"third"+", 5)");
  }

  @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_GAME_LIST);
        onCreate(db);
  }

} 

1 个答案:

答案 0 :(得分:0)

SQL中存在语法错误,例如需要'quoted'的字符串值,例如third

由于您没有获得异常,因此SQL无法运行。卸载您的应用,以便删除旧版本的数据库并再次运行onCreate()When is SQLiteOpenHelper onCreate() / onUpgrade() run?