插入错误,找不到表

时间:2013-05-05 05:34:45

标签: java android sqlite

我尝试对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();
    }

}

2 个答案:

答案 0 :(得分:1)

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

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()

请理解,在没有Unicorns的世界中,您的用户不会像您那样重新安装。重新安装应该停止建议作为解决方案。

答案 1 :(得分:0)

错误消息table login has no column named id_user已经说明了原因。

首先,要确认此问题,您应该/可以使用sqlite3来检查数据库架构:

sqlite3 /data/data/your.package/databases/xxx.db
>.schema YOUR_TABLE_NAME

如果没有名为id_user的列,只需删除数据库并重新安装应用即可解决问题。