[英]How to insert double and float values to sqlite?
Following is my db creation code.以下是我的数据库创建代码。
@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);");
}
Then here goes the adding an data point code然后这里去添加一个数据点代码
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);
}
when I call当我打电话
addGeoDataEntry(10.0,11.0,3.0f,1.1f);
it gives the following error.它给出了以下错误。 How to fix this?
如何解决这个问题?
03-14 13:57:26.910: I/Database(27910): sqlite returned: error code = 1, msg = near "1.0": syntax error
REAL
is what you are looking for. REAL
是您正在寻找的。 Documentation of SQLite datatypes SQLite 数据类型的文档
SQL Supports following types of affinities: SQL 支持以下类型的关联:
If the declared type for a column contains any of these "REAL", "FLOAT", or "DOUBLE" then the column has 'REAL' affinity.如果列的声明类型包含这些“REAL”、“FLOAT”或“DOUBLE”中的任何一个,则该列具有“REAL”亲和性。
I think you should give the data types of the column as NUMERIC or DOUBLE or FLOAT or REAL我认为您应该将列的数据类型指定为NUMERIC或DOUBLE或FLOAT或REAL
Read http://sqlite.org/datatype3.html to more info.阅读http://sqlite.org/datatype3.html了解更多信息。
Each value stored in an SQLite database has one of the following storage classes − You can use either one of them REAL or DOUBLE or BLOB or NUMERIC .存储在 SQLite 数据库中的每个值都具有以下存储类之一 - 您可以使用其中之一REAL或DOUBLE或BLOB或NUMERIC 。 Details are given below:
详情如下:
REAL : The value is a floating point value, stored as an 8-byte IEEE floating point number. REAL :该值是一个浮点值,存储为一个 8 字节的 IEEE 浮点数。
BLOB : The value is a blob of data, stored exactly as it was input. BLOB :该值是一个数据块,完全按照输入的方式存储。
INTEGER : The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. INTEGER :该值是一个有符号整数,根据值的大小存储在 1、2、3、4、6 或 8 个字节中。
NUMERIC : This column may contain values using all five storage classes. NUMERIC :此列可能包含使用所有五个存储类的值。
For more detailed information: SQLite Data Type有关更多详细信息: SQLite 数据类型
actually I think your code is just fine.. you can save those values as strings (TEXT) just like you did.. (if you want to)实际上我认为你的代码很好..你可以像你一样将这些值保存为字符串(文本)..(如果你愿意)
and you probably get the error for the System.currentTimeMillis() that might be too big for INTEGER你可能会得到 System.currentTimeMillis() 的错误,这对于 INTEGER 来说可能太大了
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.