2015-07-19 87 views
-1

嗨任何人都可以請幫助解決這個問題..android.database.sqlite.SQLiteException:沒有這樣的表:和插入錯誤

這是我DatabaseHelper.java代碼

private static final String TABLE_SEATS = "Seat"; 
private static final String KEY_IDseats = "id"; 
private static final String KEY_CREATED_ATTs = "created_at"; 
private static final String KEY_Seatstrainno = "TrainNo"; 
private static final String KEY_Seatstype = "TrainType"; 
private static final String KEY_SeatsCls = "AvailC"; 
private static final String KEY_SEATSFARE="Fare" ; 
private static final String KEY_TOTALSEATS="Total_Seats"; 


private static final String CREATE_TABLE_seats = "CREATE TABLE " + TABLE_SEATS 
     + " (" + KEY_IDseats + " integer primary key autoincrement, " 
     + KEY_Seatstrainno + " string not null," 
     + KEY_Seatstype + " string not null," 
     + KEY_SeatsCls + " string not null," 
     + KEY_SEATSFARE + " string not null," 
     + KEY_TOTALSEATS + " string not null" 
     + ");"; 

@Override 
    public void onCreate(SQLiteDatabase _db) { 

     _db.execSQL(CREATE_TABLE_seats); 
     Log.d("Train seats table ", "created"); 


    } 


public void insertTrainseats(DatabaseHelper dob, String trainno, String trainttype, String traincls,String fare,String seats) { 

SQLiteDatabase SQ = dob.getWritableDatabase(); 
ContentValues initialVa = new ContentValues(); 
initialVa.put(KEY_Seatstrainno, trainno); 
initialVa.put(KEY_Seatstype, trainttype); 
initialVa.put(KEY_SeatsCls,traincls); 
initialVa.put(KEY_SEATSFARE, fare); 
initialVa.put(KEY_TOTALSEATS, seats); 

long k=SQ.insert(TABLE_SEATS, null, initialVa); 
} 

和我的logcat示出了該誤差

8月7日至一十九日:28:06.444 2522年至2522年/ com.example.myapplication E/SQLiteLog:(1)沒有這樣的表:椅座

+1

卸載你的應用程序,以擺脫任何舊的數據庫文件。 http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto

+0

告訴我們你怎麼**實例**你的DatabaseHelper。 –

回答

0

它看起來像座位表沒有被創建。您需要確保在嘗試添加數據之前調用onCreate()。

你可能需要一個onUpgrade()方法來確保的onCreate正確調用,如果你已經更改了數據庫:

public class DatabaseHelper extends SQLiteOpenHelper { 
    private static final String DATABASE_NAME = "YOUR_DATABASE_NAME; 
    // Increment this when you make changes 
    private static final int DATABASE_VERSION = 2; 

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

    // This will delete your old database, but that's probably ok since it's not yet working. 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     database.execSQL("DROP TABLE IF EXISTS " + TABLE_SEATS); 
     onCreate(database); 
    } 


    // plus the code you have above 
    ... 
} 
+0

我在oncreate的開頭添加了一個log.d()語句,並檢查了它正在調用的天氣,但它不顯示在logcat中,意味着oncreate不會被調用no?我現在可以做什麼... [我是Android新手] –

+0

您可能需要添加onUpgrade方法。我已經在上面添加了更多的細節。 –

相關問題