I don't know why, but when I log in into my app it doesn't create the values in the SQLite database
this is my code:
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");
}
}
This is in the login fragment:
@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() {
...
While debugging, the adduser(...)
method:
long id = db.insert(TABLE_USERS, null, values);
id
is always -1 and I don't know why.
One problem is that your CREATE TABLE
is missing the SQL for the data_nascimento
column you're trying to insert values into.
After adding it, you can uninstall your app to recreate the database.
尝试添加到“ 自动增量到Id
PRIMARY KEY
列。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.