如何在运行时在Sqlite中存储聊天历史记录,如Android中的whatsapp?

时间:2015-11-19 19:42:23

标签: android sqlite crud

我创建了一个简单的聊天应用程序,现在我想将聊天消息存储在Sqlite中,以便为用户提供聊天记录。但我不知道如何创建数据库和表,以及在运行时插入数据。

我想暂时存储_sender,_receiver和_msg。我尝试了this帖子的答案,但

中存在语法错误
this.insertStmt = this.myDataBase.compileStatement(INSERT);

即使我不知道对于我的情况是否正确,因为在聊天记录中,数据库似乎有点复杂。

我尝试使用此代码:

public void createDynamicDatabase(Context context, String tableName, ArrayList<String> title)  
{
    Log.i("INSIDE createLoginDatabase() Method","*************creatLoginDatabase*********");

    try 
    {
        int i;
        String queryString;

        // Opens database in writable mode.
        myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null);          

        //System.out.println("Table Name : "+tableName.get(0));

        queryString = title.get(0)+" VARCHAR(30),";
        Log.d("**createDynamicDatabase", "in oncreate");

        for(i = 1;i < title.size() - 1; i++)
        {               
            queryString += title.get(i);
            queryString +=" VARCHAR(30)";
            queryString +=",";
        }

        queryString+= title.get(i) +" VARCHAR(30)";

        queryString = "CREATE TABLE IF NOT EXISTS " + tableName + "("+queryString+");";

        System.out.println("Create Table Stmt : "+ queryString);

        myDataBase.execSQL(queryString);
    } 
    catch (SQLException ex) 
    {
        Log.i("CreateDB Exception ",ex.getMessage());
    }
}

public void insert(Context context, ArrayList<String> array_vals, ArrayList<String> title, String TABLE_NAME) 
{
    Log.d("Inside Insert","Insertion starts for table name: "+TABLE_NAME);

    // Opens database in writable mode.
    myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null);         

    String titleString = null;
    String markString = null;

    int i;
    titleString = title.get(0)+",";
    markString = "?,";

    Log.d("**createDynamicDatabase", "in oncreate");

    for(i = 1;i < title.size() - 1; i++)
    {               
            titleString += title.get(i);
            titleString +=",";
            markString += "?,";
    }

    titleString+= title.get(i);
    markString += "?";

    //System.out.println("Title String: "+titleString);
    //System.out.println("Mark String: "+markString);

    INSERT="insert into "+ TABLE_NAME + "("+titleString+")"+ "values" +"("+markString+")";
    System.out.println("Insert statement: "+INSERT);
    //System.out.println("Array size iiiiii::: "+array_vals.size());
    //this.insertStmt = this.myDataBase.compileStatement(INSERT);
    int s=0;

    while(s<array_vals.size())
    {
        System.out.println("Size of array1"+array_vals.size());
                //System.out.println("Size of array"+title.size());
        int j=1;
        this.insertStmt = this.myDataBase.compileStatement(INSERT);
        for(int k =0;k< title.size();k++)
        {

            //System.out.println("Value of column "+title+" is "+array_vals.get(k+s));
            //System.out.println("PRINT S:"+array_vals.get(k+s));
            System.out.println("BindString: insertStmt.bindString("+j+","+ array_vals.get(k+s)+")");
            insertStmt.bindString(j, array_vals.get(k+s));



            j++;
        }

        s+=title.size();

        }
        insertStmt.executeInsert();
    }

我在onCreate()创建数据库,然后在用户发送msg和receive时插入数据。但是出现此错误。;

android.database.sqlite.SQLiteException: near "08104710280": syntax error (code 1): , while compiling: insert into 08104710280(sender,receiver,msg)values(?,?,?)

在这一行

this.insertStmt = this.myDataBase.compileStatement(INSERT);

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

在Android中,首选且更容易的方法是使用Room library,它大大减少了样板,并且可以与LiveData之类的其他体系结构组件一起流畅地工作,可以为您处理后台查询并提供仅用于几行代码。