0

我在我的 SQL lite 数据库中添加了 2 个新列,但由于没有创建新列之一,因此出现 SQL 错误。我已经阅读了具有类似错误的其他线程并仔细检查以确保我拼写正确。

问题似乎出在我的 onCreate 上,因为这是程序崩溃的地方,但我似乎无法弄清楚。我添加了 Player_Number 和 Team 列,但尚未创建 Player_Number,但 Team one 已创建。

这是我的 DBAdapter 代码:

public class PlayerDbAdapter {

private static final String DATABASE_CREATE = "CREATE TABLE Players (_id integer primary key autoincrement, Player_Name text not null, Player_Position text not null, Player_Number text not null, Team text not null);";
private static final String DATABASE_NAME = "Score";
private static final String DATABASE_TABLE = "Players";
private static final int DATABASE_VERSION = 3;
  public static final String KEY_BODY = "Player_Name";
  public static final String KEY_ROWID = "_id";
  public static final String KEY_TITLE = "Player_Position";
  public static final String KEY_NUMBER = "Player_Number";
  public static final String KEY_TEAM = "Team";
private static final String TAG = "PlayerDbAdapter";
  private final Context mCtx;
  private SQLiteDatabase mDb;
  private DatabaseHelper mDbHelper;

  public PlayerDbAdapter(Context paramContext)
  {
    this.mCtx = paramContext;
  }

  public void close()
  {
    this.mDbHelper.close();
  }


  public long createPlayers(String playerName, String playerPosition, String playerNumber, String team)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put(KEY_BODY, playerName);
    localContentValues.put(KEY_TITLE, playerPosition);
    localContentValues.put(KEY_NUMBER, playerNumber);
    localContentValues.put(KEY_TEAM, team);
    try{
        return this.mDb.insert("Players", null, localContentValues);
    } catch (Exception e) {
        Log.e("Juma", e.getMessage());
    }

    return 0;
  }

日志猫

04-17 14:41:42.890: I/Database(407): sqlite returned: error code = 1, msg = table Players has no column named Player_Number
04-17 14:41:43.011: E/Database(407): Error inserting Player_Number=23 Team=Chester 1st Team (Men) Player_Position=Goalkeeper Player_Name=a
04-17 14:41:43.011: E/Database(407): android.database.sqlite.SQLiteException: table Players has no column named Player_Number: , while compiling: INSERT INTO Players(Player_Number, Team, Player_Position, Player_Name) VALUES(?, ?, ?, ?);
04-17 14:41:43.011: E/Database(407):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-17 14:41:43.011: E/Database(407):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
04-17 14:41:43.011: E/Database(407):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
04-17 14:41:43.011: E/Database(407):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
04-17 14:41:43.011: E/Database(407):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
04-17 14:41:43.011: E/Database(407):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
04-17 14:41:43.011: E/Database(407):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
04-17 14:41:43.011: E/Database(407):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1422)
04-17 14:41:43.011: E/Database(407):    at playerdatabase.PlayerDbAdapter.createPlayer(PlayerDbAdapter.java:46)
04-17 14:41:43.011: E/Database(407):    at your.dissertation.project.SquadActivity$1.onClick(SquadActivity.java:53)
04-17 14:41:43.011: E/Database(407):    at android.view.View.performClick(View.java:2485)
04-17 14:41:43.011: E/Database(407):    at android.view.View$PerformClick.run(View.java:9080)
04-17 14:41:43.011: E/Database(407):    at android.os.Handler.handleCallback(Handler.java:587)
04-17 14:41:43.011: E/Database(407):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 14:41:43.011: E/Database(407):    at android.os.Looper.loop(Looper.java:123)
04-17 14:41:43.011: E/Database(407):    at android.app.ActivityThread.main(ActivityThread.java:3647)
04-17 14:41:43.011: E/Database(407):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 14:41:43.011: E/Database(407):    at java.lang.reflect.Method.invoke(Method.java:507)
04-17 14:41:43.011: E/Database(407):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-17 14:41:43.011: E/Database(407):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-17 14:41:43.011: E/Database(407):    at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

2

这是您使用正确语法并使用常量的查询...

private static final String DATABASE_TABLE = "Players";
private static final String DATABASE_CREATE = "CREATE TABLE "+DATABASE_TABLE+" (_id integer primary key autoincrement, Player_Name text not null, Player_Position text not null, Player_Number text not null, Team text not null);";
于 2013-04-17T15:12:30.117 回答
2
1. I think Always Double check your create table command  
   .execSQL("create table " + DATABASE_TABLE+ 
                "(" 
                + KEY_ID    + " integer primary key autoincrement,"
                + KEY_NAME  + " text not null," 
                + KEY_START + " text not null,"
                + Key_END   + " text not null,"
                + KEY_PAGE  + " text not null);"
            );

2. if you add or remove any column or change schema  then make sure you Upgrade your database version .
   using previous version through similar exception  
于 2014-08-27T06:56:32.333 回答
0

这个问题主要是由语法引起的。例如,以前我的代码是正确的。这里是:

String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "("
                    + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_MESSAGEADDRESS
                    + " TEXT," + COLUMN_MESSAGEBODY + " TEXT " + ")";

上面的代码运行正确,但我添加了另一列,它开始导致上述错误。以下是错误代码:

String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "("
                    + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_MESSAGEADDRESS
                    + " TEXT," + COLUMN_MESSAGEBODY + " TEXT" + COLUMN_MESSAGETIME + " LONG" + ")";

如您所见,当我添加新列时,我忘记在输入 TEXT 后包含逗号。这意味着当执行此代码时,将出现语法错误,因为编译器会将最后一部分读取为:

COLUMN_MESSAGEBODY TEXTCOLUMN_MESSAGETIME LONG

相对于:

COLUMN_MESSAGEBODY TEXT, COLUMN_MESSAGETIME LONG

解决方案:确保您的语法正确,并注意空格和逗号。

kindly check the below link for more info:
http://stackoverflow.com/questions/15123966/android-table-has-no-column-named-variable-name-mysql-database-error/23776738#23776738
于 2014-05-21T07:30:48.257 回答