2013-11-27 53 views
2

我嘗試創建一個表並插入一些值,但它給出了一個錯誤,並說沒有這樣的kolomn。對我來說,看起來我正在創建表格。 我在做什麼錯?Android sql列不存在

編輯:我改變了代碼t上面的代碼,但我仍然面臨同樣的問題。我評論cv.put(colEnd,「銷售」);並且錯誤消失了。所以看來問題在於創建tabel。任何其他想法?

這是我的代碼:

public class DatabaseHelper extends SQLiteOpenHelper{ 
static final String dbName="workObjectsDB"; 
static final String workTable="WorkObjects"; 
static final String colID="WorkObjectID"; 
static final String colStart="StartTime"; 
static final String colEnd="EndTime"; 

public DatabaseHelper(Context context) { 
    super(context, dbName, null,33); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE "+workTable+" (" +colID+ " INTEGER PRIMARY KEY AUTOINCREMENT , " +colStart+ " TEXT, "+colEnd+ " TEXT)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS "+workTable); 
    onCreate(db); 
} 

public void insertSome(){ 
    SQLiteDatabase db=this.getWritableDatabase(); 
    ContentValues cv=new ContentValues(); 

    cv.put(colStart, "Sales"); 
    cv.put(colEnd, "Sales"); 
    db.insert(workTable, colID, cv); 

    db.close(); 
} 

public Cursor getAll(){ 
    SQLiteDatabase db=this.getReadableDatabase(); 
    Cursor cur=db.rawQuery("SELECT * from "+workTable,new String [] {}); 

    return cur; 
} 
} 

日誌:

11-27 16:25:14.765:E/SQLiteDatabase(7103):錯誤插入結束時間=銷售開始時間=銷售 11 -27 16:25:14.765:E/SQLiteDatabase(7103):android.database.sqlite.SQLiteException:表WorkObjects沒有名爲EndTime的列:編譯時:INSERT INTO WorkObjects(EndTime,StartTime)VALUES(?,?) 11-27 16:25:14.765:E/SQLiteDatabase(7103):在android.database.sqlite.SQLiteCompiledSql.native_co mpile(Native Method) 11-27 16:25:14.765:E/SQLiteDatabase(7103):at android.database.sqlite.SQLiteCompiledSql。(SQLiteCompiledSql.java:68) 11-27 16:25:14.765:E/SQLiteDatabase(7103):在android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143) 11-27 16:25:14.765:E/SQLiteDatabase(7103):在android.database.sqlite.SQLiteProgram.compileAndbindAllArgs( SQLiteProgram.java:361) 11-27 16:25:14.765:E/SQLiteDatabase(7103):at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260) 11-27 16:25:14.765: E/SQLiteDatabase(7103):在android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112) 11-27 16:25:14.765:E/SQLiteDatabase(7103):在android.database.sqlite.SQLiteDatabase。 insertWithOnConflict(SQLiteDatabase.java:1807) 11-27 16:25:14.765 E/SQLiteDatabase(7103):at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1680) 11-27 16:25:14.765:E/SQLiteDatabase(7103) :at robin.urenapp.DatabaseHelper.insertSome(DatabaseHelper.java:39) 11-27 16:25:14.765:E/SQLiteDatabase(7103):at robin.urenapp.MainActivity.onCreate(MainActivity.java:78) 11 -27 16:25:14.765:E/SQLiteDatabase(7103):在android.app.Activity.performCreate(Activity.java:4531) 11-27 16:25:14.765:E/SQLiteDatabase(7103):在android。 E/SQLiteDatabase(7103):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150) 11-27 16:25:14.765 E/SQLiteDatabase(7103):android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150) 11-27 16: 25:14.765:E/SQLiteDatabase(7103):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.ja va:2229) 11-27 16:25:14.765:E/SQLiteDatabase(7103):at android.app.ActivityThread.access $ 600(ActivityThread.java:139) 11-27 16:25:14.765:E/SQLiteDatabase (7103):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1261) 11-27 16:25:14.765:E/SQLiteDatabase(7103):at android.os.Handler.dispatchMessage(Handler.java :99) 11-27 16:25:14.765:E/SQLiteDatabase(7103):at android.os.Looper.loop(Looper.java:154) 11-27 16:25:14.765:E/SQLiteDatabase(7103 ):at android.app.ActivityThread.main(ActivityThread.java:4945) 11-27 16:25:14.765:E/SQLiteDatabase(7103):at java.lang.reflect.Method.invokeNative(Native Method) 11 -27 16:25:14.765:E/SQLiteDatabase(7103):在java.lang.reflect.Method.invoke(Method.java:511) 11-27 16:25:14。 765:E/SQLiteDatabase(7103):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 11-27 16:25:14.765:E/SQLiteDatabase(7103):at com。 android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 11-27 16:25:14。765:E/SQLite數據庫(7103):在dalvik.system.NativeStart.main(本地方法)

回答

0

實際上它應該是:

db.execSQL("CREATE TABLE "+workTable+" (" +colID+ " INTEGER PRIMARY KEY AUTOINCREMENT , " +colStart+ " TEXT, "+colEnd+ " TEXT)"); 

但修復它之後,我不得不從33數據庫升級到34

0

您正在使用STRING作爲列數據類型; SQLite不支持STRING它支持TEXT。將STRING更改爲TEXT。另外請注意,您需要像其他人指出的那樣在此添加空間+colEnd+ " STRING)

2

根據SQLite data typesSTRING似乎不是有效的數據類型。

因此改變:

db.execSQL("CREATE TABLE "+workTable+" (" +colID+ " INTEGER PRIMARY KEY AUTOINCREMENT , " +colStart+ " STRING, "+colEnd+ "STRING)"); 

至(請注意,我把一個額外的間距colEnd後):

db.execSQL("CREATE TABLE "+workTable+" (" +colID+ " INTEGER PRIMARY KEY AUTOINCREMENT , " +colStart+ " TEXT, "+colEnd+ " TEXT)"); 
0

試試這個,每當u的分配任意值作爲主鍵使用前列名下劃線 from colId =「WorkObjectID」 To this colId =「_ workObjectID」

從STRING

和改變TEXT