我试图在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_USER = "user";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String FIRST_NAME = "firstname";
private static final String LAST_NAME = "lastname";
private static final String EMAIL = "email";
private static final String PASSWORD = "password";
private static final String USER_NAME = "username";
private static final String GENDER = "gender";
private static final String PICTURE = "picture";
private static final String STATUS = "status";
private static final String CREATED_AT = "created_at";
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_USER + "("
+ KEY_ID + " TEXT PRIMARY KEY," + FIRST_NAME + " TEXT," + LAST_NAME + " TEXT," + EMAIL + " TEXT UNIQUE,"
+ PASSWORD + " TEXT,"+ USER_NAME + " TEXT UNIQUE," + GENDER + " TEXT," + PICTURE + " TEXT," + STATUS + " TEXT,"
+ CREATED_AT + " 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_USER);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUser(String id, String firstname, String lastname, String email, String password,
String username, String gender, String picture, String status, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, id); // id
values.put(FIRST_NAME, firstname); // firstname
values.put(LAST_NAME, lastname); // lastname
values.put(EMAIL, email); // email
values.put(PASSWORD, password); // password
values.put(USER_NAME, username); // username
values.put(GENDER, gender); // gender
values.put(PICTURE, picture); // created_at
values.put(STATUS, status); // picture
values.put(CREATED_AT, created_at); // status
// Inserting Row
long id_inserted = db.insert(TABLE_USER, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id + " | "+id_inserted);
}
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_USER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("idUser", cursor.getString(0));
user.put("firstname", cursor.getString(1));
user.put("lastname", cursor.getString(2));
user.put("email", cursor.getString(3));
user.put("password", cursor.getString(4));
user.put("username", cursor.getString(5));
user.put("gender", cursor.getString(6));
user.put("picture", cursor.getString(7));
user.put("status", cursor.getString(8));
user.put("created_at", cursor.getString(9));
}
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_USER, null, null);
db.close();
Log.d(TAG, "Deleted all user info from sqlite");
}
}
但是当我尝试添加新列(下面的代码)时,插入不起作用,它返回-1,我不知道如何,请你帮助我找到它返回-1的原因以及为什么数据未插入我的sqlite表
答案 0 :(得分:1)
您确定您的CREATE_LOGIN_TABLE列已正确创建吗?看起来你在create语句中有拼写错误。
+ TOKEN + "TEXT)"
实际应该是
+ TOKEN + " TEXT)"
如果有帮助,请告诉我。