android sqlite - 插入语法错误(代码1)

时间:2014-07-19 15:45:29

标签: sqlite syntax insert

我无法弄清楚我的代码中出了什么问题:

    package com.example.lirans88;

import java.util.ArrayList;
import java.util.List;

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

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

    // Messages table name
    private static final String TABLE_MESSAGES = "messages";

    // Messages Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_TEXT = "msg";
    private static final String KEY_FROM = "from";
    private static final String KEY_TO = "to";

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

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_MESSAGES_TABLE = "CREATE TABLE " + TABLE_MESSAGES + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TEXT + " TEXT," + KEY_FROM + " TEXT,"
                + KEY_TO + " TEXT" + ");";
        db.execSQL(CREATE_MESSAGES_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_MESSAGES);

        // Create tables again
        onCreate(db);
    }

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

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

        ContentValues values = new ContentValues();
        values.put(KEY_ID, 1);
        values.put(KEY_TEXT, "hello");
        values.put(KEY_FROM, "123");
        values.put(KEY_TO, "456");

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


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

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

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                MessageItem message = new MessageItem();
                message.setId(cursor.getString(0));
                message.setText(cursor.getString(1));
                message.setFrom(cursor.getString(2));
                message.setTo(cursor.getString(3));
                // Adding message to list
                messageList.add(message);
            } while (cursor.moveToNext());
        }

        // return message list
        return messageList;
    }

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

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

}

使用我的应用程序中的addMessage函数将数据插入表格时,我收到以下错误:

07-19 11:35:59.129:E / SQLiteLog(1656):( 1)接近&#34;到&#34;:语法错误 07-19 11:35:59.179:E / SQLiteDatabase(1656):插入错误= 456 id = 1 from = 123 msg = hello 07-19 11:35:59.179:E / SQLiteDatabase(1656):android.database.sqlite.SQLiteException:near&#34; to&#34 ;:语法错误(代码1):,同时编译:INSERT INTO消息(到,id,from,msg)VALUES(?,?,?,?)

注意:我暂时将值放在ContentValues对象中以排除我应用的其他部分的问题。

由于

1 个答案:

答案 0 :(得分:3)

tofrom是SQL中的保留关键字。 要将它们用作标识符,它们应该是""双引号。 或者更好的是,只需重命名列。

您应该已经在onCreate()中遇到语法错误,所以显然它还没有运行。卸载您的应用程序,以便删除旧的数据库文件,并调用具有适当列名的onCreate()