向已经工作的数据库添加更多列(添加了加号和减号)。我看过以前的答案,但我不能为我的生活找到我的语法错误。
private static final String TAG = "DBAdapter";
// DB Fields
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
/*
* CHANGE 1:
*/
// TODO: Setup your fields here:
public static final String KEY_NUMBER = "number";
public static final String KEY_LASTNAME = "lastname";
public static final String KEY_FIRSTNAME = "firstname";
public static final String KEY_POSITION = "position";
public static final String KEY_TIMEONICE = "timeonice";
public static final String KEY_PLUS = "plus";
public static final String KEY_MINUS = "minus";
// TODO: Setup your field numbers here (0 = KEY_ROWID, 1=...)
public static final int COL_NUMBER = 1;
public static final int COL_LASTNAME = 2;
public static final int COL_FIRSTNAME = 3;
public static final int COL_POSITION = 4;
public static final int COL_TIMEONICE = 5;
public static final int COL_PLUS = 6;
public static final int COL_MINUS = 7;
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NUMBER, KEY_LASTNAME, KEY_FIRSTNAME, KEY_POSITION, KEY_TIMEONICE,KEY_PLUS,KEY_MINUS};
// DB info: it's name, and the table we are using (just one).
public static final String DATABASE_NAME = "HOCKEY.db";
public static final String DATABASE_TABLE = "mainTable";
// Track DB version if a new version of your app changes the format.
public static final int DATABASE_VERSION = 7;
private static final String DATABASE_CREATE_SQL =
"create table " + DATABASE_TABLE
+ " (" + KEY_ROWID + " integer primary key autoincrement, "
/*
* CHANGE 2:
*/
// TODO: Place your fields here!
// + KEY_{...} + " {type} not null"
// - Key is the column name you created above.
// - {type} is one of: text, integer, real, blob
// (http://www.sqlite.org/datatype3.html)
// - "not null" means it is a required field (must be given a value).
// NOTE: All must be comma separated (end of line!) Last one must have NO comma!!
+ KEY_NUMBER + " integer not null,"
+ KEY_LASTNAME + " string not null,"
+ KEY_FIRSTNAME + " string not null,"
+ KEY_POSITION + " string not null,"
+ KEY_TIMEONICE + " string"
+ KEY_PLUS + " integer,"
+ KEY_MINUS + " integer"
// Rest of creation:
+ ");";
记录猫
01-01 21:55:16.971:E / AndroidRuntime(3476):进程:com.example.timeonice,PID:3476 01-01 21:55:16.971:E / AndroidRuntime(3476):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.timeonice / com.example.timeonice.MainActivity}:android.database.sqlite.SQLiteException :没有这样的列:加(代码1):,同时编译:SELECT DISTINCT _id,number,lastname,firstname,position,timeonice,plus,minus FROM mainTable 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.os.Handler.dispatchMessage(Handler.java:102) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.os.Looper.loop(Looper.java:136) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.app.ActivityThread.main(ActivityThread.java:5017) 01-01 21:55:16.971:E / AndroidRuntime(3476):at java.lang.reflect.Method.invokeNative(Native Method) 01-01 21:55:16.971:E / AndroidRuntime(3476):at java.lang.reflect.Method.invoke(Method.java:515) 01-01 21:55:16.971:E / AndroidRuntime(3476):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 01-01 21:55:16.971:E / AndroidRuntime(3476):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 01-01 21:55:16.971:E / AndroidRuntime(3476):at dalvik.system.NativeStart.main(Native Method) 01-01 21:55:16.971:E / AndroidRuntime(3476):引起:android.database.sqlite.SQLiteException:没有这样的列:加(代码1):,编译时:SELECT DISTINCT _id,number,lastname,firstname ,position,timeonice,plus,minus FROM mainTable 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) 01-01 21:55:16.971:E / AndroidRuntime(3476):at com.example.timeonice.DBAdapter.getAllRows(DBAdapter.java:159) 01-01 21:55:16.971:E / AndroidRuntime(3476):at com.example.timeonice.DBAdapter.deleteAll(DBAdapter.java:146) 01-01 21:55:16.971:E / AndroidRuntime(3476):at com.example.timeonice.MainActivity.onCreate(MainActivity.java:135) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.app.Activity.performCreate(Activity.java:5231) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 01-01 21:55:16.971:E / AndroidRuntime(3476):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
答案 0 :(得分:3)
此后您缺少逗号:
+ KEY_TIMEONICE + " string"
应该是:
+ KEY_TIMEONICE + " string,"
答案 1 :(得分:2)
你错过了一个逗号
+ KEY_TIMEONICE + " string, " // <-- Here.