当我尝试将一些数据插入到我在android中创建的SQLliteDatabase时,我遇到了问题。
下面是我创建数据库的代码,我在一个名为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 emailAdd=t@n.com
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 );
我无法理解它,这让我发疯了!感谢您抽出时间查看这个问题。