Android中的SQLite Insert语句;
下面是我编写的SQLite表的声明语句Android中的SQLite Insert语句;,android,sqlite,Android,Sqlite,下面是我编写的SQLite表的声明语句 db.execSQL("create table if not exists data( d text, hr integer, rrInterval real, acc_x real, acc_y real, acc_z real);"); 这就是表数据的样子 data{ d text, hr integer, rrInterval real, acc_x real, acc_y real, acc_z
db.execSQL("create table if not exists data( d text, hr integer, rrInterval real, acc_x real, acc_y real, acc_z real);");
这就是表数据的样子
data{
d text,
hr integer,
rrInterval real,
acc_x real,
acc_y real,
acc_z real
}
我得到了一些用微软Band 2测量的变量,存储在以下变量中;我检查了所有这些变量,从MS波段获得了正确的数据
String d;
int heartRate;
double rrInterval;
float acc_x, acc_y, acc_z;
所以这一个是用来在SQLite数据库中存储数据的
MyOpenHelper helper = new MyOpenHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("create table if not exists data( d text, hr integer, rrInterval real, acc_x real, acc_y real, acc_z real);");
/* some other codes irrelevant to DB */
db.execSQL("insert into data(d, hr, rrInterval, acc_x, acc_y, acc_z) values ('" +
d + "', " + //date
Integer.toString(heartRate) + ", " + //hr
Double.toString(rrInterval)+ ", " + //rrInterval
Float.toString(acc_x) + ", " + //acc_x
Float.toString(acc_y) + ", " + //acc_y
Float.toString(acc_z) + //acc_z
");");
据我所知,Java和SQLite上都没有语法错误。但是Android Studio返回错误,这不是insert…
语句。我该如何解决这个问题呢?不要使用那种方法,尝试使用ContentValues,因为它更容易使用
ContentValues cv = new ContentValues();
cv.put("d", DATA_NEEDED_TO_INSERT);
cv.put("hr", DATA_NEEDED_TO_INSERT);
cv.put("rrInterval", DATA_NEEDED_TO_INSERT);
cv.put("acc_x", DATA_NEEDED_TO_INSERT);
cv.put("acc_y", DATA_NEEDED_TO_INSERT);
cv.put("acc_z", DATA_NEEDED_TO_INSERT);
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, cv); // insert function
db.close(); // dont forget to close your db
别忘了关闭数据库();过程结束后,欢呼声“但是Android Studio返回错误”,返回什么错误?你为什么不使用SQLiteDatabase#insert()
方法呢?你的代码对我来说很好。我使用的是Android Studio 2.3.2。该代码无法编译:没有SQLiteDatabase#put()
方法对不起,我的错,我把它改为insert