我嘗試將一些數據插入到我在android中創建的SQLliteDatabase中時出現問題。如何插入到SQLliteDatabase android?
下面是我的代碼來創建,我在一個叫SQLlitenew類創建的數據庫:
public class SQLlitenew{
public static final String KEY_ROWID="id";
public static final String KEY_PRODUCTNAME="ProductName";
public static final String KEY_PRODUCTTYPE="ProductType";
public static final String KEY_LOCATION="place";
public static final String KEY_WORKER="worker";
public static final String KEY_QUESTION1="Question1";
public static final String KEY_QUESTION2="Question2";
public static final String KEY_QUESTION3="Question3";
public static final String KEY_QUESTION4="Question4";
public static final String KEY_QUESTION5="Question5";
public static final String KEY_TIME="time";
public static final String KEY_UPLOADED="uploaded";
public static final String KEY_EMAIL="emailAdd";
public static final String TAG="DBAdapter"; // This is for logging information e.g LOG.d
private static final String DATABASE_NAME="NOID";
private final static String DATABASE_TABLE="session";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE = "create table if not exists " + DATABASE_TABLE + " (id INT(11) primary key autoincrement, " +
"ProductName VARCHAR, ProductType VARCHAR, place INT(5), worker VARCHAR, " +
"Question1 VARCHAR, Question2 VARCHAR, Question3 VARCHAR, Question4 VARCHAR, Question5 VARCHAR, " +
"time long, uploaded INT(1), emailAdd VARCHAR);";
//create instant of DBhelper class
private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase ourDatabase) {
// TODO Auto-generated method stub
//First create the database table
try{
ourDatabase.execSQL(DATABASE_CREATE);
Log.w(DATABASE_CREATE,"successssssssssssssss!");
}catch(SQLException e){
e.printStackTrace();
Log.w(DATABASE_CREATE,"FFFAAAAIIIIIILLLLLLEEEDDDDDDDDDDDD!");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
//----always move the Data before Deleting the Data----->
Log.w(TAG, "This is will delete the previous data which will make you lose all information from the" +oldVersion + "and will create a new database to the " + newVersion);
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
onCreate(db);
}
}
//set up public contructor
public SQLlitenew(Context c){
this.ourContext = c;
ourHelper = new DBHelper(c);
}
public long insertRecord(String productName, String productType, String place, String name, String ans1, String ans2, String ans3, String firstChoice, String secondChoice, long time, int uploaded, String email){
//writing stuff into db
ContentValues content = new ContentValues();
content.put(KEY_PRODUCTNAME, productName);
content.put(KEY_PRODUCTTYPE, productType);
content.put(KEY_LOCATION, place);
content.put(KEY_WORKER, name);
content.put(KEY_QUESTION1, ans1);
content.put(KEY_QUESTION2, ans2);
content.put(KEY_QUESTION3, ans3);
content.put(KEY_QUESTION4, firstChoice);
content.put(KEY_QUESTION5, secondChoice);
content.put(KEY_TIME, time);
content.put(KEY_UPLOADED, uploaded);
content.put(KEY_EMAIL, email);
return ourDatabase.insert(DATABASE_TABLE, null, content);
}
public void close(){
ourHelper.close();
}
public SQLiteDatabase getWritableDatabase() {
ourHelper = new DBHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
// TODO Auto-generated method stub
return null;
}
}
這是我調用該函數將數據插入一個單獨的類中的代碼:
SQLlitenew entry = new SQLlitenew(this);
entry.open();
entry.insertRecord(product, productType, place, name, ans1, ans2, ans3, firstChoice, secondChoice, time, uploaded, email);
entry.close();
的問題是,當過我嘗試插入,它帶來的這個錯誤:
12-18 13:08:45.029: I/Database(4282): sqlite returned: error code = 1, msg = no such table: session
12-18 13:08:45.044: E/Database(4282): Error inserting time=1355864924948 ProductType=Food Question5=Fresh & Clean Scents i.e nourishing milk & honey that hydrates and protects
12-18 13:08:45.044: E/Database(4282): uploaded=0 Question4=Chip resistant nail polish
12-18 13:08:45.044: E/Database(4282): worker=ttt Question3=YES ProductName=tyyyy Question2=African American Question1=Under 25 place=56677 [email protected]
12-18 13:08:45.044: E/Database(4282): android.database.sqlite.SQLiteException: no such table: session: , while compiling: INSERT INTO session(time, ProductType, Question5, uploaded, Question4, worker, Question3, ProductName, Question2, Question1, place, emailAdd) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
個上提高database_version到3
12-18 14:00:38.794: I/Database(4384): sqlite returned: error code = 1, msg = AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
12-18 14:00:38.794: E/Database(4384): Failure 1 (AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY) on 0x279960 when preparing 'create table if not exists session (id INT(11) primary key autoincrement, ProductName VARCHAR, ProductType VARCHAR, place INT(5), worker VARCHAR, Question1 VARCHAR, Question2 VARCHAR, Question3 VARCHAR, Question4 VARCHAR, Question5 VARCHAR, time long, uploaded INT(1), emailAdd VARCHAR);'.
12-18 14:00:38.802: W/System.err(4384): android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: create table if not exists session (id INT(11) primary key autoincrement, ProductName VARCHAR, ProductType VARCHAR, place INT(5), worker VARCHAR, Question1 VARCHAR, Question2 VARCHAR, Question3 VARCHAR, Question4 VARCHAR, Question5 VARCHAR, time long, uploaded INT(1), emailAdd VARCHAR);
新的錯誤我不出來,它已經快把我逼瘋了!感謝您的時間查看這個問題。
你重命名你的表嗎?如果是這樣,你是否記得更改數據庫版本,以刪除/重新創建? DATABASE_TABLE值是「會話」嗎? – fedepaol
yes DATABASE_TABLE is =「session」。這是我唯一的名字。 database_version = 2 – user875139
嗯,你發佈的看起來很好。請發佈您的SQLiteOpenHelper子類「onCreate()'和'onUpgrade()'方法。 – Sam