继续得到错误sqlite代码1

时间:2016-02-05 04:30:55

标签: android sqlite

有人可以告诉该代码有什么问题无法解决。

它告诉我表没有列。 感谢

public class DatabaseHandlerTest extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "MessagesManager";

// messages table name
private static final String TABLE_MESSAGE = "message";


// messages Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_USER_NAME = "user_name";
private static final String KEY_USER_MESSAGE = "user_messgae";


public DatabaseHandlerTest(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_MESSAGE_TABLE =
            "CREATE TABLE "
                    + TABLE_MESSAGE + "("
                    + KEY_ID + " INTEGER PRIMARY KEY,"
                    + KEY_USER_NAME + " TEXT,"
                    + KEY_USER_MESSAGE + " TEXT" + ")";
  /* String CREATE_CONTACTS_TABLE =
            "CREATE TABLE "
                     + TABLE_CONTACTS + "("
                     + KEY_ID + " INTEGER PRIMARY KEY,"
                     + KEY_NAME + " TEXT,"
                     + KEY_PH_NO + " TEXT" + ")";*/
    db.execSQL(CREATE_MESSAGE_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_MESSAGE);

    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new message
void addMessage(ChatMessage msg) {
    SQLiteDatabase db = this.getWritableDatabase();


    ContentValues values = new ContentValues();

    values.put(KEY_USER_NAME, msg.getmUsername());
    values.put(KEY_USER_MESSAGE, msg.getmUserMessge());

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

// Getting single message
public ChatMessage getMessage(String id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_MESSAGE, new String[]{
                    KEY_ID,
                    KEY_USER_NAME,
                    KEY_USER_MESSAGE}, KEY_ID + "=?",
            new String[]{id}, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    ChatMessage msg = new ChatMessage();
    msg.setmId(Integer.parseInt(cursor.getString(0)));
    msg.setmUsername(cursor.getString(6));
    msg.setmUserMessge(cursor.getString(7));

    // return message
    return msg;
}

// Getting All messages
public List<ChatMessage> getAllMessages() {
    List<ChatMessage> messageList = new ArrayList<>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_MESSAGE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            ChatMessage msg = new ChatMessage();

            msg.setmId(Integer.parseInt(cursor.getString(0)));
            msg.setmUsername(cursor.getString(1));
            msg.setmUserMessge(cursor.getString(2));
            // Adding message to list
            messageList.add(msg);
        } while (cursor.moveToNext());
    }


    // return message list
    return messageList;
}

// Updating single message
public int updateMessage(ChatMessage msg) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_USER_NAME, msg.getmUsername());
    values.put(KEY_USER_MESSAGE, msg.getmUserMessge());

    // updating row
    return db.update(TABLE_MESSAGE, values, KEY_ID + " = ?",
            new String[]{String.valueOf(msg.getmId())});
}

// Deleting single message
public void deleteMessage(ChatMessage msg) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_MESSAGE, KEY_ID + " = ?",
            new String[]{String.valueOf(msg.getmId())});
    db.close();
}


// Getting messages Count
public int getMessageCount() {
    String countQuery = "SELECT  * FROM " + TABLE_MESSAGE;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

}

02-04 23:17:26.830 14306-14306/com.android.myshull E/SQLiteLog: (1) table message has no column named user_name
02-04 23:17:26.830 14306-14306/com.android.myshull E/SQLiteDatabase: Error inserting user_name=Test user_messgae=TEST
                                                                         android.database.sqlite.SQLiteException: table message has no column named user_name (code 1): , while compiling: INSERT INTO message(user_name,user_messgae) VALUES (?,?)
                                                                             at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                             at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                             at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                             at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                             at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                             at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                             at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                             at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                             at com.tal.android.myshull.DatabaseHandlerTest.addMessage(DatabaseHandlerTest.java:83)
                                                                             at com.tal.android.myshull.TestDataBaseFragment$1.onClick(TestDataBaseFragment.java:53)
                                                                             at android.view.View.performClick(View.java:5198)
                                                                             at android.view.View$PerformClick.run(View.java:21147)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:1)

检查了代码,它确实在数据库中创建了表,

private static final String KEY_ID = "id";
private static final String KEY_USER_NAME = "user_name";
private static final String KEY_USER_MESSAGE = "user_messages";

String CREATE_MESSAGE_TABLE =
        "CREATE TABLE "
                + " message " + "( "
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_USER_NAME + " TEXT, "
                + KEY_USER_MESSAGE + " TEXT" + ")";

您可能希望卸载应用程序以清除数据库并重新安装,

答案 1 :(得分:0)

我认为@Pankaj Nimgade是对的,每当您更改sqlite结构时,您都必须清除您的应用数据。因为如果存在同名的表,sqlite不会检查表中的任何更改。它只是假设表没有变化。