2012-02-28 55 views
0

我試圖插入數據到一個大型項目的一部分表中。我收到上面的錯誤,無法弄清楚如何創建表。創建表的代碼在下面,表名稱是檢查。所以我很困惑。數據庫(3300):sqlite返回:錯誤代碼= 1,msg =沒有這樣的表:但表名是檢查?

這裏是logcat的信息

02-27 18:38:45.956: I/Database(3559): sqlite returned: error code = 1, msg = no such table:  inspections 
02-27 18:38:45.967: E/Database(3559): Error inserting co_driver= driver=ewrre vehicle_id=ET 432 status=1 date_time=40 vehicle_type=truck 
02-27 18:38:45.967: E/Database(3559): android.database.sqlite.SQLiteException: no such table: inspections: , while compiling: INSERT INTO inspections(co_driver, driver, vehicle_id, status, date_time, vehicle_type) VALUES(?, ?, ?, ?, ?, ?); 
02-27 18:38:45.967: E/Database(3559): at android.view.View.performClick(View.java:2408) 

下面是創建該表

private static final String DATABASE_CREATE = "create table inspections (" 
    + "_id integer primary key autoincrement, " 
    + "vehicle_id string not null, " 
    + "vehicle_type string not null, " 
    + "datetime  integer not null, " 
    + "driver  string not null, " 
    + "codriver  string , " 
    + "status  integer not null " 
    + ");"; 

public static void onCreate(SQLiteDatabase database) { 
    database.execSQL(DATABASE_CREATE); 
} 

public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 
    Log.w(InspectionsTable.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); 
    database.execSQL("DROP TABLE IF EXISTS inspections"); 
    onCreate(database); 
} 

這是DBhelper類

public class SignalSetDBHelper extends SQLiteOpenHelper { 
private static final String DATABASE_NAME = "db"; 
private static final int DATABASE_VERSION = 1; 

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

// Method is called during creation of the database 
@Override 
public void onCreate(SQLiteDatabase db) { 
    CurrentStateTable.onCreate(db); 
    HoursOfServiceTable.onCreate(db); 
    InspectionsTable.onCreate(db); 
    } 

// Method is called during an upgrade of the database, 
// e.g. if you increase the database version 
@Override 
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 
    CurrentStateTable.onUpgrade(database, oldVersion, newVersion); 
    HoursOfServiceTable.onUpgrade(database, oldVersion, newVersion); 
} 
} 

這是適配器類

代碼
public class InspectionDBAdapter { 
private static final String KEY_ROWID ="_id"; 
private static final String KEY_VEHICLE_ID="vehicle_id"; 
private static final String KEY_VEHICLE_TYPE="vehicle_type";  
private static final String KEY_DATETIME="date_time"; 
private static final String KEY_DRIVER="driver"; 
private static final String KEY_CO_DRIVER="co_driver"; 
private static final String KEY_STATUS="status"; 
private static final String DB_TABLE="inspections"; 
private Context context; 
private SQLiteDatabase db; 
private SignalSetDBHelper dbHelper; 

public InspectionDBAdapter(Context context) { 
    this.context = context; 
} 

public InspectionDBAdapter open() throws SQLException { 
    dbHelper = new SignalSetDBHelper(context); 
    db = dbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    dbHelper.close(); 
} 

public void insertInspection(String vehicle_id, String vehicle_type, 
     int datetime, String driver, String codriver, int status) 
{ 

    ContentValues newContact= createContentValue(vehicle_id, vehicle_type, 
     datetime, driver, codriver, status); 

    db.insert(DB_TABLE, KEY_STATUS, newContact); 

} 

private static ContentValues createContentValue(String vehicle_id, String vehicle_type, 
     int datetime, String driver, String codriver, int status) 
{ 
    ContentValues newContact= new ContentValues(); 
    newContact.put(KEY_VEHICLE_ID , vehicle_id); 
    newContact.put(KEY_VEHICLE_TYPE , vehicle_type); 
    newContact.put(KEY_DATETIME , datetime); 
    newContact.put(KEY_DRIVER , driver); 
    newContact.put(KEY_CO_DRIVER , codriver); 
    newContact.put(KEY_STATUS , status); 

    return newContact; 
+0

您可以在創建表格檢查時分享代碼嗎? – 2012-02-28 03:21:44

+0

我剛剛添加了創建表的代碼。表名是檢查,所以這就是爲什麼我感到困惑。類名是InspectionTable。我試過了,它不起作用 – Aaron 2012-02-28 03:27:24

+0

你可以發佈你創建table的類的完整代碼嗎?我想,你已經擴展了SQLiteOpenHelper類。 – Hiral 2012-02-28 04:42:08

回答

2

嘗試使你的SignalSetDBHelper類,如:

public class SignalSetDBHelper extends SQLiteOpenHelper { 

    private static final String DATABASE_NAME = "db"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String INSPECTION_CREATE = "create table inspections (" 
    + "_id integer primary key autoincrement, " 
    + "vehicle_id string not null, " 
    + "vehicle_type string not null, " 
    + "datetime  integer not null, " 
    + "driver  string not null, " 
    + "codriver  string , " 
    + "status  integer not null " 
    + ");"; 

    public SignalSetDBHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    }  
    @Override 
    public void onCreate(SQLiteDatabase db) { 

     database.execSQL(INSPECTION_CREATE); 
     //do same for other two tables 
    }    
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 

     database.execSQL("DROP TABLE IF EXISTS inspections"); 
     // do same for other two tables 
     onCreate(database);   
    } 
} 

而在你的SignalSetDBHelper類的代碼,在onUpgrade方法,你不包括InspectionsTable.onUpgrade(數據庫,oldVersion,NEWVERSION);並且在所有語句之後,您還應該添加onCreate(database);請嘗試將其添加到您現有的代碼中,並查看獲得的成功結果!

+0

謝謝,我一直在這一整天都在努力。 – Aaron 2012-02-28 05:56:56

+0

@Aaron:很高興幫助:) – Hiral 2012-02-28 06:11:47

相關問題