How to use rawQuery() method to insert a record?

前端 未结 4 1607
挽巷
挽巷 2020-12-18 00:36

I need to store a record using rawQuery() method, because I want to insert the current date and time (datetime()), but I also need to insert string

相关标签:
4条回答
  • 2020-12-18 01:07

    I solved the problem with in this way:

    String sql="INSERT INTO sms VALUES (null,?,?,?,datetime('NOW'))";
    dbw.execSQL(sql,new Object[]{mitt,dest,text});
    

    Finally I can store every char without problems!!!

    0 讨论(0)
  • 2020-12-18 01:10
    String sql="INSERT INTO sms VALUES ( null, '"+str1+"', '"+str2+"', '"+str3+"', datetime())";
    dbw.rawQuery(sql, null);
    

    Mark the single quotes.

    0 讨论(0)
  • 2020-12-18 01:16

    SQLite doesn't have a native datetime data storage type. It can store a string or an integer representation of a date instead.

    To convert your values you can use the date time functions detailed in Sqlite Date and Time functions documentation

    Your initial attempt is almost correct, but your datetime() function call requires an argument of 'NOW'.

    String sql="INSERT INTO sms VALUES ( null, ?, ?, ?, datetime('NOW'))";
    

    Also you should call execSQL instead of rawQuery which is expecting to return a recordset.

    dbw.execSQL(sql, new String[]{str1,str2,str3});
    

    You can alo specify individual columns to insert data into by inserting a field list after the table name in your query if not inserting all the values

    String sql = "INSERT INTO sms(f1, f2, f3, f4)"
               + "VALUES ( null, ?, ?, ?, datetime('NOW'))";
    

    Another option that may be possible is using a default timestamp in SQLite ,although I have not attempted this in android.

    0 讨论(0)
  • 2020-12-18 01:26

    Try it like this:

    ContentValues values;
    values=new ContentValues();
    
    // values.put("field_name",value);
    values.put("id", 5); 
    values.put("name", name);
    dbw.insert("table_name", null, values);
    
    0 讨论(0)
提交回复
热议问题