2012-06-02 50 views
0

我有下面的代碼,其收集從在佈局字段中的數據(同一類)並嘗試把它放在msqlite數據庫內:錯誤時插入到MSQLite

public void submitOrder(Order order_) { 
    SQLiteDatabase db=DBHelper.getWritableDatabase(); 
    ContentValues cv=new ContentValues(); 
     cv.put(DatabaseHelper.colName,  order_.name); 
     cv.put(DatabaseHelper.colLink,  order_.link); 
     cv.put(DatabaseHelper.colPrice,  order_.price); 
     cv.put(DatabaseHelper.colDateYear, order_.year); 
     cv.put(DatabaseHelper.colDateMonth, order_.month); 
     cv.put(DatabaseHelper.colDateDay, order_.day); 

     db.insert(DatabaseHelper.orderTable, null, cv); 
    db.close(); 

和我有MSQlite數據庫幫助類我已經創建了:

package com.Sagi.MyOrders; 

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

public class DatabaseHelper extends SQLiteOpenHelper { 

static final String dbName="DBname"; 
static final String orderTable="Orders"; 
static final String colName="OrderName"; 
static final String colLink="OrderLink"; 
static final String colDateYear="DateYear"; 
static final String colDateMonth="DateMonth"; 
static final String colDateDay="DateDay"; 
static final String colPrice="OrderPrice"; 
static String CREATE_T; 

public DatabaseHelper(Context context) { 
     super(context, dbName, null,1); 
} 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(SQLiteDatabase db) { 
     CREATE_T="CREATE TABLE orderTable (" 
       + "colName TEXT," 
       + "colLink TEXT," 
       + "colPrice TEXT," 
       + "colDateYear INTEGER," 
       + "colDateMonth INTEGER," 
       + "colDateDay INTEGER);"; 
     db.execSQL(CREATE_T); 
     Log.e("Table creator","Table Created successfully"); 
    } 

當我運行它,然後點擊按鈕存儲在數據庫中的信息,我得到一個錯誤信息:

06-02 06:19:43.454: E/Database(1570): android.database.sqlite.SQLiteException: table Orders has no column named OrderPrice: , while compiling: INSERT INTO Orders(DateDay, DateMonth, OrderLink, OrderName, OrderPrice, DateYear) VALUES(?, ?, ?, ?, ?, ?); 

和I級消息:

06-02 06:19:43.444: I/Database(1570): sqlite returned: error code = 1, msg = table Orders has no column named OrderPrice 

希望你能幫助...謝謝!

+0

清除數據或者反安裝應用程序,然後它會工作 –

回答

1

它說table Orders has no column named OrderPrice。原因是你不更新你的數據庫。只需卸載/重新安裝您的應用程序。一切都會好的。

您的編輯後:

您創建

CREATE_T="CREATE TABLE orderTable (" 
       + "colName TEXT," 
       + "colLink TEXT," 
       + "colPrice TEXT," 
       + "colDateYear INTEGER," 
       + "colDateMonth INTEGER," 
       + "colDateDay INTEGER);"; 
     db.execSQL(CREATE_T); 

這意味着你創建一個表名爲orderTable你的表。

但在你插入:

public void submitOrder(Order order_) { 
    SQLiteDatabase db=DBHelper.getWritableDatabase(); 
    ContentValues cv=new ContentValues(); 
     cv.put(DatabaseHelper.colName,  order_.name); 
     cv.put(DatabaseHelper.colLink,  order_.link); 
     cv.put(DatabaseHelper.colPrice,  order_.price); 
     cv.put(DatabaseHelper.colDateYear, order_.year); 
     cv.put(DatabaseHelper.colDateMonth, order_.month); 
     cv.put(DatabaseHelper.colDateDay, order_.day); 

     db.insert(DatabaseHelper.orderTable, null, cv); // This line 
    db.close(); 

您使用DatabaseHelper.orderTable這是static final String orderTable="Orders"; 他們是不相等的,所以你收到此錯誤。

更改orderTable變量或表名,那麼你會沒事的

希望這有助於

+0

嗯,這似乎給我的另一個步驟, 我現在得到以下錯誤: 06-02 06:50:02.646:我/數據庫(328):sqlite返回:錯誤代碼= 1,msg =沒有這樣的表:訂單 – SagiLow

+0

我在你的評論後編輯我的答案。希望這可以幫助 –

+0

謝謝。 我看到它處理得不好,當我創建我的表格列使用增量, 所以我改變它的字符串,它現在所有的作品.. 太糟糕了,由字符串列名更有組織。 – SagiLow