我想在数据库中创建新条目。 这是我的数据库类:
公共类DataBase {
private static final String DATABSE_NAME = "MY_DB";
private static final String DATABSE_TABLE_LOGIN = "tblLogin";
private static final int DATABASE_VERSION = 1;
public static final String DUM="DUM";
public static final String BOOKER_ID = "BookerID";
public static final String BNAME = "BName";
public static final String PASS = "Pass";
String[] Columns_Login = new String[]{DUM,BOOKER_ID,BNAME,PASS};
public static final String DATABASE_CREATE = " CREATE TABLE " + DATABSE_TABLE_LOGIN + " (" +
DUM + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
BOOKER_ID+" TEXT NOT NULL, "+
BNAME + " TEXT NOT NULL, " +
PASS + " TEXT NOT NULL) " ;
private DatabaseHelper myHelper;
private final Context mycontext;
private SQLiteDatabase myDB;
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABSE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABSE_TABLE_LOGIN);
onCreate(db);
}
}
public DataBase(Context c) {
mycontext = c;
}
public DataBase open() throws SQLException {
myHelper = new DatabaseHelper(mycontext);
myDB = myHelper.getWritableDatabase();
return this;
}
public void close() throws SQLException {
myHelper.close();
}
public long createEntry(String _BID, String _BName, String _Pass) throws SQLException {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(BOOKER_ID, _BID);
cv.put(BNAME,_BName);
cv.put(PASS,_Pass);
return myDB.insert(DATABSE_TABLE_LOGIN, null, cv);
}
public Boolean CheckLogin(String _BID, String _Pass) throws SQLException{
Boolean Y_N =false ;
// Cursor c = myDB.rawQuery("select * from tblLogin where BookerID="+_BID+" and Pass="+_Pass,null);
Cursor c2 = myDB.query(DATABSE_TABLE_LOGIN, Columns_Login, BOOKER_ID + "=? AND "+ PASS+"=?", new String[]{_BID,_Pass}, null, null, null);
if(c2.getCount()>0){
Y_N=true ;
}
return Y_N;
}
}
当我调用CreatEntry()方法时,它会给我以下错误:
android.database.sqlite.SQLiteException: table tblLogin has no column named BookerID: , while compiling:
错误在哪里? 而且当我调用CheckLogin方法时,它也会说没有名为BookerID的列。
答案 0 :(得分:1)
快速修正:
答案 1 :(得分:1)
首先检查数据库是否按照您希望的方式创建。 如果没有,你可以将你的db版本购买增加1。 AS
private static final int DATABASE_VERSION = 2;
然后运行该程序。
更新db版本时,它将执行onUpgrade()方法并对db应用新的更改
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABSE_TABLE_LOGIN);
onCreate(db);
}
答案 2 :(得分:0)
更改此BOOKER_ID +“ TEXT NOT NULL,” + 在BOOKER_ID之后进行制表 谢谢