我正在使用PHP脚本填充我的SQLiteDatabase以获取登录凭据和帐户信息。即使登录工作正常,SQLite在我的性别名称列中也有错误我不知道我对此缺少什么。有人可以为我指出它吗? Cu'z me和我的朋友找不到它,并且都是Android Development的新手。
这是错误
> E/SQLiteDatabase﹕ Error inserting gender=tuesdaycross04@gmail.com bday=24242 email=Male address=asdasd mobile=1995-03-03
> full_name=Arnold
> android.database.sqlite.SQLiteException: table accounts has no column named gender (code 1): , while compiling: INSERT INTO
> accounts(gender,bday,email,address,mobile,full_name) VALUES
> (?,?,?,?,?,?)
SQLiteHelper
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "brm_dbs";
// Login table name
private static final String TABLE_LOGIN = "accounts";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "full_name";
private static final String KEY_ADDRESS = "address";
private static final String KEY_MOBILE = "mobile";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";
private static final String KEY_BDAY = "bday";
private static final String KEY_GENDER = "gender";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_ADDRESS + " TEXT," + KEY_MOBILE + " INTEGER,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
+ KEY_CREATED_AT + " TEXT," + KEY_BDAY + " TEXT,"
+ KEY_GENDER + " TEXT" + ");";
db.execSQL(CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
public void getloginUser(String full_name, String address,
String bday, String gender,
String mobile, String email) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(KEY_UNIQUE_ID, unique_id);
values.put(KEY_NAME, full_name); // Name
values.put(KEY_ADDRESS, address); // address
values.put(KEY_BDAY, bday); // BDAY
values.put(KEY_GENDER, gender); // GENDER
values.put(KEY_MOBILE, mobile); // mobile
values.put(KEY_EMAIL, email); // Email
// Inserting Row
long id = db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
通过登录凭据插入SQLite
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
JSONObject user = jObj.getJSONObject("user");
String full_name = user.getString("full_name");
String address = user.getString("address");
String mobile = user.getString("mobile");
String email = user.getString("email");
String bday = user.getString("bday");
String gender = user.getString("gender");
// Inserting row in users table
db.getloginUser(full_name, address, mobile, email, bday, gender);
// Launch main activity
// Launch main activity
Intent intent = new Intent(Loign.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
答案 0 :(得分:1)
应插入值,表格的创建顺序为......
答案 1 :(得分:1)
打开DDMS并删除应用程序数据,否则擦除您的模拟器并运行您的应用程序,它将正常工作。该表已经存在,没有性别列,当你正在运行的应用程序时,因为DB退出然后它不会调用你的SQLiteOpenHelper的onCreate方法,那么在DB之后你做了什么改变它可能不会影响表的结构。
<强>解决方案:强>
从模拟器中卸载应用程序并尝试从Android工作室运行该应用程序,
将diff数据库版本设为2或以前的数据库版本+ 1。然后onUpgrade()方法将调用编写代码来更改现有表的结构,或添加所需的任何表或根据您的要求删除表。