gpt4 book ai didi

java - 插入错误且未找到表

转载 作者:行者123 更新时间:2023-12-01 14:37:11 28 4
gpt4 key购买 nike

我尝试对 android sqlite 数据库进行查询以创建表并将数据插入数据库。

创建表函数

public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_ID_USER + " TEXT,"
+ KEY_NAMA + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_REGID + " TEXT,"
+ KEY_DIBUAT_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
}

插入数据功能

public void addUser(String id_user, String name, String email, String regid, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAMA, name); // Name
values.put(KEY_ID_USER, id_user);
values.put(KEY_EMAIL, email); // Email
values.put(KEY_REGID, regid); // Email
values.put(KEY_DIBUAT_AT, created_at); // Created At

// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
}

我认为这段代码是正确的,但我会运行他的包含错误

05-05 11:19:15.549: E/SQLiteDatabase(29294): Error inserting regid=XX1 id_user=12 nama=Edwin Andrianto dibuat_at=2013-05-01 15:11:41 email=edwinuad@gmail.com

05-05 11:19:15.549: E/SQLiteDatabase(29294): android.database.sqlite.SQLiteException: table login has no column named id_user: , while compiling: INSERT INTO login(regid,id_user,nama,dibuat_at,email) VALUES (?,?,?,?,?)

有谁知道上面的语法有什么问题吗?谢谢

完整代码DatabaseHandler.java

package net.drieanto.lagidimana.library;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

// 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_LOGIN = "login";

// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_ID_USER = "id_user";
private static final String KEY_NAMA = "nama";
private static final String KEY_EMAIL = "email";
private static final String KEY_REGID = "regid";
private static final String KEY_DIBUAT_AT = "dibuat_at";

public DatabaseHandler(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_ID_USER + " TEXT,"
+ KEY_NAMA + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_REGID + " TEXT,"
+ KEY_DIBUAT_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);

// Create tables again
onCreate(db);
}

/**
* Storing user details in database
* */
public void addUser(String id_user, String name, String email, String regid, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAMA, name); // Name
values.put(KEY_ID_USER, id_user);
values.put(KEY_EMAIL, email); // Email
values.put(KEY_REGID, regid); // Email
values.put(KEY_DIBUAT_AT, created_at); // Created At

// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
}

/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("id_user", cursor.getString(1));
user.put("name", cursor.getString(2));
user.put("email", cursor.getString(3));
user.put("regid", cursor.getString(4));
user.put("dibuat_at", cursor.getString(5));
}
cursor.close();
db.close();
// return user
return user;
}

/**
* Getting user login status
* return true if rows are there in table
* */
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();

// return row count
return rowCount;
}

/**
* Re crate database
* Delete all tables and create them again
* */
public void resetTables(){
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_LOGIN, null, null);
db.close();
}

}

最佳答案

如果您不时编辑这部分代码,就会出现此问题

String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_ID_USER + " TEXT,"
+ KEY_NAMA + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_REGID + " TEXT,"
+ KEY_DIBUAT_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);

没有使用正确的 onUpgrade 函数。简单的清除数据或重新安装可以解决问题,但要获得更干净且正确的解决方案,请参阅: SQLite onUpgrade()

请理解,在没有 unicorn 的世界中,您的用户不太可能像您一样重新安装。确实应该停止建议重新安装作为解决方案。

关于java - 插入错误且未找到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16381719/

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