尝试在android sqlite中动态创建的表中插入值时出错 [英] Error when trying to insert values in dynamically created table in android sqlite

查看:22
本文介绍了尝试在android sqlite中动态创建的表中插入值时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务中有以下功能:

I have following function in my service:

private void saveToDB(String sender, String message, String time) {
    DBHandler db = new DBHandler(getApplicationContext(), sender);
    db.insertOrCreateTable(sender, message, time);
}

这是我的数据库处理程序类:

This is my db handler class:

public class DBHandler extends SQLiteOpenHelper {
    // All Static variables

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Mo";
    private static String mSender;

    private static final String KEY_ID = "id";
    private static final String KEY_SENDER ="sender";
    private static final String KEY_MSG = "msg";
    private static final String KEY_TIME ="time";

    //constructor
    public DBHandler(Context context, String sender) {
        //higher db version when user needs to create new table
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        SQLiteDatabase db = this.getWritableDatabase();
        mSender = sender;
    }

    // Contacts table name
    private static final String TABLE_MESSAGES = mSender;

    private String CREATE_MESSAGES_TABLE = "Create Table " + TABLE_MESSAGES + 
              "("   
            + KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_SENDER + " TEXT, "
            + KEY_MSG + " TEXT, "
            + KEY_TIME + " TEXT" + ")";



    // Creating Tables
    @Override 
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_MESSAGES_TABLE);
    }

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


    public void insertOrCreateTable(String s,String m,String t) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_SENDER, s); // Message Sender
        values.put(KEY_MSG, m); // Message Message
        values.put(KEY_TIME, t); // Message Time

        // Inserting Row
        db.insert(mSender, null, values);
        db.close();
    }
}

现在,我尝试动态创建表,但它总是返回错误:

Now, I try to create tables dynamically but it always returns an error:

    E/SQLiteLog: (1) near "65": syntax error
    03-09 12:03:13.813 20828-21609/com.example.asus.service E/SQLiteDatabase: Error inserting sender=65 time=12:03 msg=C
                                                                              android.database.sqlite.SQLiteException: near "65": syntax error (

code 1): , while compiling: INSERT INTO 65(sender,time,msg) VALUES (?,?,?)

有什么想法或建议吗?我也以不同的方式在一个线程中发布了这个,所以也可以随意查看它(可能会帮助你更好地理解我的问题)这是链接:如何动态添加表在android sqlite中?

Any Ideas or suggestions? Also I posted this in a thread in a different way so feel free to look at it too (might help u understand my problem better) Here's the link: How do I dynamically add tables in android sqlite?

伙计们,我做错了什么?

What am I doing wrong guys?

推荐答案

如果你的表名为 65 你应该使用 ' ' 插入.而不是:

If your table has name 65 you should use ' ' to insert. Instead of:

INSERT INTO 65(sender,time,msg) VALUES (?,?,?)

试试:

INSERT INTO '65'(sender,time,msg) VALUES (?,?,?)

这篇关于尝试在android sqlite中动态创建的表中插入值时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆