我不知道为什么,但是当我登录我的应用时,它并没有在SQLite数据库中创建值
这是我的代码:
public class SQLiteHandler extends SQLiteOpenHelper {
private static final String TAG = SQLiteHandler.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "android_api";
// Login table name
private static final String TABLE_USERS = "users";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "nome";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_DATA_NASCIMENTO = "data_nascimento";
private static final String KEY_DATA_CRIADO = "user_datacriado";
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_USERS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
+ KEY_DATA_CRIADO + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUser(String nome, String email, String uid, String data_nascimento, String user_datacriado) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, nome); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_UID, uid); // Email
values.put(KEY_DATA_NASCIMENTO, data_nascimento); // Created At
values.put(KEY_DATA_CRIADO, user_datacriado); // Created At
// Inserting Row
long id = db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_USERS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("nome", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
/**
* Re crate database Delete all tables and create them again
* */
public void deleteUsers() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_USERS, null, null);
db.close();
Log.d(TAG, "Deleted all user info from sqlite");
}
}
这是在登录片段中:
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
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);
// Now store the user in SQLite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("nome");
String email = user.getString("email");
String data_nascimento = user.getString("data_nascimento");
String user_datacriado = user.getString("user_datacriado");
// Inserting row in users table
db.addUser(name, email, uid, data_nascimento, user_datacriado);
// Launch main activity
//Intent intent = new Intent(LoginActivity.this,
//MainActivity.class);
//startActivity(intent);
//finish();
// Launch login activity
Intent intent = new Intent(
getActivity(),
MainActivity.class);
startActivity(intent);
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
...
调试时,使用adduser(...)
方法:
long id = db.insert(TABLE_USERS, null, values);
id
总是 -1 ,我不知道为什么。
答案 0 :(得分:1)
一个问题是,您的CREATE TABLE
缺少您尝试插入值的data_nascimento
列的SQL。
添加后,您可以卸载应用程序以重新创建数据库。
答案 1 :(得分:-1)
尝试将Autoincrement添加到Id
PRIMARY KEY
列。