gpt4 book ai didi

Android SQLiteDatabase 插入错误

转载 作者:行者123 更新时间:2023-11-29 15:05:58 25 4
gpt4 key购买 nike

我试图在 SQLiteDatabase 中存储一些信息。当我调用方法 insert 时,它返回 -1。所以发生了错误,但我无法弄清楚哪里出了问题。

代码如下:

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 = "Find";

// 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 KEY_NAME = "name";
private static final String KEY_LAST_NAME = "last_name";
private static final String KEY_EMAIL = "email";
private static final String KEY_BIRTHDAY = "birthday";
private static final String KEY_DESCRIPTION = "description";
private static final String KEY_CITY = "city";
private static final String KEY_UID = "uid";
private static final String KEY_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 + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_LAST_NAME + " TEXT," + KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_BIRTHDAY + "TEXT" + KEY_DESCRIPTION + "TEXT"
+ KEY_CITY + "TEXT" + KEY_UID + " TEXT,"
+ KEY_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 name, String last_name, String email, String birthday, String description,
String city, String uid, String created_at) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_LAST_NAME, last_name); // Last Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_BIRTHDAY, birthday); // Birthday
values.put(KEY_DESCRIPTION, description); // Description
values.put(KEY_CITY, city); // City
values.put(KEY_UID, uid); // Unique ID
values.put(KEY_CREATED_AT, created_at); // Created at

// Inserting Row
long id = db.insert(TABLE_USER, 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 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("name", cursor.getString(1));
user.put("last_name", cursor.getString(2));
user.put("email", cursor.getString(3));
user.put("birthday", cursor.getString(4));
user.put("description", cursor.getString(5));
user.put("city", cursor.getString(6));
user.put("uid", cursor.getString(7));
user.put("created_at", cursor.getString(8));
}
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");
}

在此先感谢您的帮助。

最佳答案

你创建的 SQL 是错误的,你错过了 ',' 和空格,使用下面的。

String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + " ("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_LAST_NAME + " TEXT," + KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_BIRTHDAY + " TEXT," + KEY_DESCRIPTION + " TEXT,"
+ KEY_CITY + " TEXT," + KEY_UID + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ");";

关于Android SQLiteDatabase 插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37419137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com