我有这个数据库代码:
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "fname";
public static final String KEY_LNAME = "lname";
public static final String KEY_MNAME = "mname";
public static final String KEY_CONTACTNUM = "contactNum";
public static final String KEY_LICENSE = "license";
public static final String KEY_USER = "username";
public static final String KEY_PASS = "password";
private static final String DATABASE_NAME = "sampleForFinals";
private static final String DATABASE_TABLE = "drivers";
private static final int DATABASE_VERSION = 1;
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);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(" CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ROWID + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_FNAME + "TEXT NOT NULL, " +
KEY_LNAME + "TEXT NOT NULL, " +
KEY_MNAME + "TEXT NOT NULL, " +
KEY_CONTACTNUM + "TEXT NOT NULL, " +
KEY_LICENSE + "TEXT NOT NULL, " +
KEY_USER + "TEXT NOT NULL, " +
KEY_PASS + "TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + DATABASE_NAME);
onCreate(db);
}
}
public DatabaseHelper(Context c) {
ourContext = c;
}
public DatabaseHelper open() {
ourHelper = new DBHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long savedata(String fname, String lname, String mname, String contactNum, String license, String user, String pass) {
ContentValues cv = new ContentValues();
cv.put(KEY_FNAME, fname);
cv.put(KEY_LNAME, lname);
cv.put(KEY_MNAME, mname);
cv.put(KEY_CONTACTNUM, contactNum);
cv.put(KEY_LICENSE, license);
cv.put(KEY_USER, user);
cv.put(KEY_PASS, pass);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
这是我的register.java
register.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String fname = firstname.getText().toString();
String lname = lastname.getText().toString();
String mname = middlename.getText().toString();
String contact = contactNum.getText().toString();
String plate = license.getText().toString();
String user = username.getText().toString();
String pass = password.getText().toString();
DatabaseHelper save = new DatabaseHelper(Register.this);
save.open();
save.savedata(fname,lname, mname, contact, plate, user, pass);
firstname.setText("");
lastname.setText("");
middlename.setText("");
contactNum.setText("");
license.setText("");
username.setText("");
password.setText("");
}
});
基本上,错误说的是:
table drivers has no column named mname
inserting mname=a username=a lname=a license=a password=a contactNum=4 fname=a
android.database.sqlite.SQLiteException: table drivers has no column named mname (code 1): , while compiling: INSERT INTO drivers(mname,username,lname,license,password,contactNum,fname) VALUES (?,?,?,?,?,?,?)
所以,我使用的数据库来自FITACADEMY。这是一个sqlite。我可以在FITACADEMY中看到数据库表和列以及是的mname列。我能看到它。但是,错误说它没有列'mname'。我试图删除与'mname'相关的所有代码,然后进入没有列的'user'。因此,我删除了与'user'相关的所有代码,然后再次出现错误,即没有列'fname'。 注意:这是将数据插入数据库。
我的代码出了什么问题?我现在已经发现了3个小时的错误。我还是想不通。
答案 0 :(得分:2)
您需要在CREATE TABLE
SQL中的列名称及其类型之间包含空格。例如:
KEY_MNAME + "TEXT NOT NULL, " +
需要
KEY_MNAME + " TEXT NOT NULL, " +
与所有其他列相同的问题。更新SQL后,卸载您的应用以使onCreate()
再次执行。
答案 1 :(得分:0)
您是否更新了数据库方案?在这种情况下,您需要在此处增加数据库版本号:
private static final int DATABASE_VERSION = 1;
答案 2 :(得分:0)
您在创建表查询时遇到语法错误,因此我更正了您的查询尝试此
" CREATE TABLE " + DATABASE_TABLE + "( " +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_FNAME + " TEXT NOT NULL, " +
KEY_LNAME + " TEXT NOT NULL, " +
KEY_MNAME + " TEXT NOT NULL, " +
KEY_CONTACTNUM + " TEXT NOT NULL, " +
KEY_LICENSE + " TEXT NOT NULL, " +
KEY_USER + " TEXT NOT NULL, " +
KEY_PASS + " TEXT NOT NULL);"
将数据库版本增加1。 享受:)
另外还有一个onUpgrade方法是丢弃数据库而不是删除表:
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);