Android SQLite表插入

时间:2014-02-13 16:05:14

标签: android sql sqlite

我已经获得此表单,用户可以在其中跟踪自己的位置并为每个事件指定昵称。我正在收集该昵称并使用它来尝试在该数据库中使用该名称形成一个新表。

我要么

 table does not exist 

the data is added to the previous table without creating a new one.

要将此表添加到当前数据库需要做什么?

以下是整个班级:

  public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_NAME = MainActivity.nickName;
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_COMMENT = "comment";
  public static final String COLUMN_LAT = "lat";
  public static final String COLUMN_LONG = "long";
  public static final String COLUMN_RADI = "radi";




 private static final String DATABASE_NAME = "logs.db";
 private static final int DATABASE_VERSION = 1;



  // Database creation sql statement
   private static final String DATABASE_CREATE = "create table "
      + TABLE_NAME + "(" + COLUMN_ID
      + " integer primary key autoincrement, " + COLUMN_COMMENT
      + " text not null," + COLUMN_LAT+ ","  + COLUMN_LONG + "," + COLUMN_RADI +    ");";


  MySQLiteHelper(Context context) {
  super(context, context.getExternalFilesDir(null).getAbsolutePath() + "/" +  DATABASE_NAME, null, DATABASE_VERSION);
 }

  @Override
  public void onCreate(SQLiteDatabase database) {
      database.execSQL(DATABASE_CREATE);


}

 @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     Log.w(MySQLiteHelper.class.getName(),
      "Upgrading database from version " + oldVersion + " to "
        + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
onCreate(db);
 }

}  

2 个答案:

答案 0 :(得分:1)

尝试以下查询:

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_STARTDATE + " TEXT, " + KEY_STARTTIME + " TEXT, "
+ KEY_TRIALID + " TEXT, " + KEY_TRIALNAME + " TEXT, " + KEY_ENDDATE + " TEXT, "+    KEY_ENDTIME +  " TEXT, "  + KEY_USERNAME +  " TEXT"+")";


        values.put(KEY_STARTDATE, item.getStartdate());
values.put(KEY_STARTTIME, item.getStarttime());
values.put(KEY_TRIALID, item.getTrialid());
values.put(KEY_TRIALNAME, item.getTrialname());
values.put(KEY_ENDDATE, item.getEnddate());
values.put(KEY_ENDTIME, item.getEndtime());
values.put(KEY_USERNAME, item.getUsername());

答案 1 :(得分:1)

您需要定义一种将数据插入数据库的方法。

您还应该阅读有关此类事情的最佳做法。一个非常好的起点是:

http://www.vogella.com/tutorials/AndroidSQLite/article.html