android-如何将double和float值插入sqlite?
以下是我的数据库创建代码。
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TIME + " INTEGER, " +
LONGI + " TEXT, "+
LATI + " TEXT, "+
SPEED + " TEXT, "+
ACCU + " TEXT);");
}
然后在这里添加数据点代码
private void addGeoDataEntry(double logi, double lati, float speed, float accu) {
SQLiteDatabase db = gpsDataHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TIME, System.currentTimeMillis());
values.put(LONGI, logi+"");
values.put(LATI, lati+"");
values.put(SPEED, speed+"");
values.put(ACCU, accu+"");
db.insertOrThrow(TABLE_NAME, null, values);
}
当我打电话
addGeoDataEntry(10.0,11.0,3.0f,1.1f);
它给出以下错误。 如何解决这个问题?
03-14 13:57:26.910: I/Database(27910): sqlite returned: error code = 1, msg = near "1.0": syntax error
dinesh707 asked 2019-12-29T18:51:59Z
5个解决方案
103 votes
REAL是您所要的。 SQLite数据类型的文档
WarrenFaith answered 2019-12-29T18:52:19Z
13 votes
SQL支持以下类型的关联:
文本
数字
整数
真实
BLOB
如果列的声明类型包含以下任何“ REAL”,“ FLOAT”或“ DOUBLE”,则该列具有“ REAL”相似性。
Sandhu answered 2019-12-29T18:53:04Z
5 votes
我认为您应该将列的数据类型指定为NUMERIC或DOUBLE或FLOAT或REAL
阅读[http://sqlite.org/datatype3.html]了解更多信息。
Chandra Sekhar answered 2019-12-29T18:53:29Z
1 votes
实际上,我认为您的代码很好。.您可以像保存代码那样将这些值另存为字符串(TEXT)。(如果需要)
并且您可能会收到System.currentTimeMillis()的错误,对于INTEGER来说可能太大
Joe answered 2019-12-29T18:53:53Z
-4 votes
enter code here
package in.my;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
private static final String DATABASE_NAME = "helper.db";
private static final int DATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
private static final String Table_Record =
"create table Student (_id integer primary key autoincrement, "
+ "Name text not null,rate integer, Phone text not null,Salary text not null,email text not null,address text not null,des text not null,qual text not null,doj text not null);";
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(Table_Record);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return DBAdapter.this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
public long insertTitle(String name,String phone,String web,String des,String address,String doj,String qual,String sal,int rate)
{
ContentValues initialValues = new ContentValues();
initialValues.put("Name", name);
initialValues.put("Phone", phone);
initialValues.put("email", web);
initialValues.put("des", des);
initialValues.put("Salary", sal);
initialValues.put("qual", qual);
initialValues.put("address", address);
initialValues.put("doj", doj);
initialValues.put("rate", rate);
return db.insert("Student", null, initialValues);
}
public boolean deleteTitle(long rowId)
{
return db.delete("Student", KEY_ID +
"=" + rowId, null) > 0;
}
public boolean UpdateTitle(long id,String name,String phone,String web,String des,String address,String doj,String qual,String sal,int rate)
{
ContentValues initialValues = new ContentValues();
initialValues.put("Name", name);
initialValues.put("Phone", phone);
initialValues.put("email", web);
initialValues.put("des", des);
initialValues.put("qual", qual);
initialValues.put("Salary", sal);
initialValues.put("address", address);
initialValues.put("doj", doj);
initialValues.put("rate", rate);
return db.update("Student",initialValues, KEY_ID + "=" + id, null)>0;
//return db.insert("Student", null, initialValues);
}
public Cursor getAllRecords()
{
return db.query("Student", new String[] {
KEY_ID,
"Name",
"Phone",
"email",
"address",
"des",
"qual",
"doj",
"Salary",
"rate"
},
null,
null,
null,
null,
null);
}
}
Aashish Bhatnagar answered 2019-12-29T18:54:09Z