【发布时间】:2015-10-12 05:13:07
【问题描述】:
我正在使用 PHP 脚本填充我的 SQLiteDatabase,以获取登录凭据和帐户信息。即使登录工作正常,SQLite 在列中我的性别名称也有错误,我不知道我缺少什么。有人可以为我指出吗?因为我和我的朋友找不到它,而且都是 Android 开发新手。
这是错误
> 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();
}
【问题讨论】:
-
编辑:没关系,刚刚意识到你所有的数据都是混合的。
-
谢谢,但仍然收到列错误
-
私有静态最终 int DATABASE_VERSION = 2;并重新构建应用程序
-
如果您最近在运行应用程序后添加了
gender列,您需要删除数据库以便使用新列重新创建它。