[英]Android SQLite table insert
I've got this form where a user can track their position and give a nickname to each event. 我有这样的表格,用户可以跟踪他们的位置并给每个事件一个昵称。 I'm collecting that nickname and using it to try to form a new table in the database with that name. 我正在收集该昵称并使用它来尝试在该数据库中使用该名称形成一个新表。
I either get a 我要么得到一个
table does not exist
or 要么
the data is added to the previous table without creating a new one.
What needs to be done to get this to add a new table to the current database? 要将此表添加到当前数据库,需要做些什么?
Here is the entire class: 这是整个班级:
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);
}
}
Try the following query: 请尝试以下查询:
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());
You need to define a method for inserting data into your database. 您需要定义一种将数据插入数据库的方法。
You should also read up on the best practices for this type of thing. 您还应该阅读有关此类事情的最佳做法。 A really good place to start is: 一个非常好的起点是:
http://www.vogella.com/tutorials/AndroidSQLite/article.html http://www.vogella.com/tutorials/AndroidSQLite/article.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.