像Android中的whatsapp一样,如何在运行时在Sqlite中存储聊天记录? [英] How to store chat history in Sqlite at runtime like whatsapp in Android?

查看:126
本文介绍了像Android中的whatsapp一样,如何在运行时在Sqlite中存储聊天记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a simple chat application and now I want to store the chat messages in Sqlite to provide chat history to users. But I don't have idea how to create database and tables, and inserting data at runtime as well.

我现在想存储_sender,_receiver和_msg。我尝试了 帖子的答案,但是其中存在语法错误

I want to store _sender, _receiver and _msg for now. I tried this post's answer but there was syntax error in

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

即使我不知道这是否适合我的情况,在聊天记录中似乎数据库也是如此有点复杂。

Even I do not know if it is right approach for my case as in chat history it seems the database is a bit complex.

我尝试使用以下代码:

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和接收消息时插入数据。但是出现此错误。

I created database in onCreate() and than insert the data when user sends msg and receives.but getting this error.;

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


推荐答案

查看SQLiteOpenHelper:

Look at SQLiteOpenHelper:

http://developer.android.com /reference/android/database/sqlite/SQLiteOpenHelper.html

编码示例:

< a href = http://www.vogella.com/tutorials/AndroidSQLite/article.html rel = nofollow> http://www.vogella.com/tutorials/AndroidSQLite/article.html

这篇关于像Android中的whatsapp一样,如何在运行时在Sqlite中存储聊天记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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