问题描述
我很新的到Android编程和得到一个错误我无法弄清楚。我basicly写了一个code,让用户发送他的当前坐标到本地SQLite数据库。这冲动发生在我开始将数据插入到dataabse。
我一直getting-
android.database.sqlite.SQLiteException:近CREATE_RECORD_TABLE:语法错误(code 1),在编译:CREATE_RECORD_TABLE(ID INTEGER PRIMARY KEY AUTOINCREMENT,纬度TEXT,经度TEXT,准确性TEXT,时间TEXT)
和我是pretty确保它发生混淆我与不同类型的很多原因。我会很高兴,如果你可以看看,并找出我到正确的方向。
该记录(表)活动:
包com.program.android.taskir;
公共类记录{ //私有变量
私人诠释身份证;
私人双PLONG;
私人双人开发平台;
私人浮动PACC;
私人长期PTIME; 公共记录(){}
//空的构造 //构造
公共记录(双PLONG,双开发平台,浮PACC,长PTIME){
超();
this.pLong = PLONG;
this.pLat =开发平台;
this.pAcc = PACC;
this.pTime = PTIME;
} @覆盖
公共字符串的toString(){
返回记录[ID =+编号+,经度=+ PLONG +,纬度=+开发平台+准确性+ PACC +,时间+ PTIME
+];
}
//获取ID
公众诠释的getID(){
返回this.id;
} //设置ID
公共无效SETID(INT ID){
this.id = ID;
} //获取PLONG
公共双getpLong(){
返回this.pLong;
} //设置PLONG
公共无效setpLong(双PLONG){
this.pLong = PLONG;
} //获取开发平台
公共双getpLat(){
返回this.pLat;
} //设置高原
公共无效setpLat(双PLAT){
this.pLat =开发平台;
} //获取PACC
公众持股量getpAcc(){
返回this.pAcc;
} //设置PACC
公共无效setpAcc(浮动PACC){
this.pAcc = PACC;
} //获取PTIME
众长getpTime(){
返回this.pTime;
} //设置PTIME
公共无效setpTime(长PTIME){
this.pTime = PTIME;
}
}
SQLite的活动:
包com.program.android.taskir;进口android.content.ContentValues;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;进口java.util.LinkedList中;
进口的java.util.List;公共类MySQLiteHelper扩展SQLiteOpenHelper { //数据库版本
私有静态最终诠释DATABASE_VERSION = 1; //数据库名称
私有静态最后弦乐DATABASE_NAME =RecordsDB; 公共MySQLiteHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
} @覆盖
公共无效的onCreate(SQLiteDatabase DB){
// SQL语句创建记录表
串CREATE_RECORD_TABLE =CREATE_RECORD_TABLE(+
ID INTEGER PRIMARY KEY AUTOINCREMENT,+
纬度TEXT,+
东经TEXT,+
精度TEXT,+
时间文本); //创建books表
db.execSQL(CREATE_RECORD_TABLE);
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
如果存在//删除旧的书表
db.execSQL(DROP TABLE IF EXISTS记录); //创建新的记录表
this.onCreate(DB);
} //书籍表名
私有静态最后弦乐TABLE_RECORD =记录; //书籍表列名
私有静态最后弦乐KEY_ID =ID;
私有静态最后弦乐KEY_LONG =经度;
私有静态最后弦乐KEY_LAT =纬度;
私有静态最后弦乐KEY_ACC =精度;
私有静态最后弦乐KEY_TIME =时间; 私有静态最后的String [] = COLUMNS {KEY_ID,KEY_LONG,KEY_LAT,KEY_ACC,KEY_TIME}; 公共无效的addRecord(录音记录){
//用于记录
Log.d(addBook,record.toString()); // 1.使用参考DB写
SQLiteDatabase分贝= this.getWritableDatabase(); // 2.创建ContentValues添加键列/值
ContentValues值=新ContentValues();
values.put(KEY_LONG,record.getpLong());
values.put(KEY_LAT,record.getpLat());
values.put(KEY_ACC,record.getpAcc());
values.put(KEY_TIME,record.getpTime()); // 3.插入
db.insert(TABLE_RECORD,//表
空,// nullColumnHack
值); //键/值 - >键=列名/值=列值 // 4.关闭
db.close();
} 公共记录getRecord(INT ID){ // 1.使用参考可读DB
SQLiteDatabase分贝= this.getReadableDatabase(); // 2.构建查询
光标光标=
db.query(TABLE_RECORD,//一个表
柱,// B。列名
ID =?,// C。选
新的String [] {将String.valueOf(ID)} //ð。选择ARGS
空,//即通过...分组
空,// F。有
空,//克为了通过
空值); // H。限制 // 3。如果我们得到的结果获取第一个
如果(指针!= NULL)
cursor.moveToFirst(); // 4.构建book对象
录制录制=新记录();
record.setID(的Integer.parseInt(cursor.getString(0)));
record.setpLat(cursor.getDouble(1));
record.setpLong(cursor.getDouble(2));
record.setpAcc(cursor.getFloat(2));
record.setpTime(cursor.getLong(2)); //日志
Log.d(getBook(+ ID +),record.toString()); // 5.归还书
返回记录;
} 公开名单<记录和GT; getAllRecords(){
清单<记录和GT;记录=新的LinkedList<记录和GT;(); // 1.构建查询
查询字符串=SELECT * FROM+ TABLE_RECORD; // 2.获取参考DB写
SQLiteDatabase分贝= this.getWritableDatabase();
光标光标= db.rawQuery(查询,NULL); // 3。去了每一行,建书,并将其添加到列表
录制录制= NULL;
如果(cursor.moveToFirst()){
做{
记录=新记录();
record.setID(的Integer.parseInt(cursor.getString(0)));
record.setpLat(cursor.getDouble(1));
record.setpLong(cursor.getDouble(2));
record.setpAcc(cursor.getFloat(2));
record.setpTime(cursor.getLong(2));
//添加书书
records.add(记录);
}而(cursor.moveToNext());
} Log.d(getAllRecords(),record.toString()); //还书
返回记录;
} 公众诠释UpdateRecords(录音记录){ // 1.使用参考DB写
SQLiteDatabase分贝= this.getWritableDatabase(); // 2.创建ContentValues添加键列/值
ContentValues值=新ContentValues();
values.put(纵横,record.getpLat()); //
values.put(经度,record.getpLong());
values.put(准确性,record.getpAcc());
values.put(时间,record.getpTime()); // 3.更新一行
INT I = db.update(TABLE_RECORD,//表
值,//列/值
KEY_ID +=,//选
新的String [] {将String.valueOf(record.getID())}); //选择ARGS // 4.关闭
db.close(); 返回我; } 公共无效deleteRecords(录音记录){ // 1.使用参考DB写
SQLiteDatabase分贝= this.getWritableDatabase(); // 2.删除
db.delete(TABLE_RECORD,//表名
KEY_ID +=,//选
新的String [] {将String.valueOf(record.getID())}); //选择ARGS // 3。关闭
db.close(); //日志
Log.d(deleteBook,record.toString()); }}
主要活动:
包com.program.android.taskir;进口android.content.Context;
进口android.content.Intent;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.support.v7.app.ActionBarActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.CompoundButton;
进口android.widget.Switch;
进口android.widget.TextView;
进口android.widget.Toast;
公共类GpsPage扩展ActionBarActivity { TextView的TZ;
TextView的textLat;
TextView的textLong;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_gps_page); 最后MySQLiteHelper DB =新MySQLiteHelper(本); TZ =(的TextView)findViewById(R.id.textviewtz);
意向意图= getIntent();
串TeudatZeut = intent.getStringExtra(TZ);
tz.setText(תעודתזהות+ TeudatZeut); 开关onOffSwitch =(开关)findViewById(R.id.switch1);
onOffSwitch.setChecked(假);
onOffSwitch.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){ Toast.makeText(getApplicationContext(),שולחנתוניםלשרת,Toast.LENGTH_SHORT).show();
textLat =(的TextView)findViewById(R.id.textLat);
textLong =(的TextView)findViewById(R.id.textLong); LM的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener的LL =新myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LL);
}
类myLocationListener实现LocationListener的{ @覆盖
公共无效onLocationChanged(地点){
如果(位置!= NULL){
双PLONG = location.getLongitude();
双PLAT = location.getLatitude();
浮PACC = location.getAccuracy();
长PTIME = location.getTime();
textLat.setText(Double.toString(高原));
textLong.setText(Double.toString(PLONG));
db.addRecord(新记录(PLONG,开发平台,PACC,PTIME));
}
} @覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){ } @覆盖
公共无效onProviderEnabled(字符串提供商){ } @覆盖
公共无效onProviderDisabled(字符串提供商){ }
}
}
);
/ **
* CRUD操作
* * / //添加记录
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.menu_gps_page,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId(); // noinspection SimplifiableIfStatement
如果(ID == R.id.action_settings){
返回true;
} 返回super.onOptionsItemSelected(项目);
}
}
logcat的报告 -
12月12日至29日:10:29.400:D / addBook(894):记录[ID = 0,经度= 1.0,纬度= 3.0,Accuracy20.0,Time1419847204000]
十二月12日至29日:10:29.470:D / addBook(894):记录[ID = 0,经度= 1.0,纬度= 3.0,Accuracy20.0,Time1419847204000]
十二月12日至29日:10:29.510:D / addBook(894):记录[ID = 0,经度= 1.0,纬度= 3.0,Accuracy20.0,Time1419847204000]
十二月12日至29日:10:29.540:D / dalvikvm(894):GC_FOR_ALLOC释放112K,9%的游离4333K / 4728K,暂停28ms,总34ms
十二月12日至29日:10:29.580:D / addBook(894):记录[ID = 0,经度= 1.0,纬度= 3.0,Accuracy20.0,Time1419847204000]
十二月12日至29日:10:29.620:D / addBook(894):记录[ID = 0,经度= 1.0,纬度= 3.0,Accuracy20.0,Time1419847204000]
十二月12日至29日:10:33.860:D / dalvikvm(385):GC_FOR_ALLOC释放761K,21%免费6394K / 8004K,暂停62ms,70ms的总
你犯的错误是在的SQLite
活动的的onCreate(SQLiteDatabase DB)
方法。
你的数据库查询应如下:
字符串CREATE_RECORD_TABLE =CREATE TABLE记录(+
ID INTEGER PRIMARY KEY AUTOINCREMENT,+
北纬文本NOT NULL,+
东经文本NOT NULL,+
精度TEXT NOT NULL,+
时间文本NOT NULL);
有关数据库的完全成熟的例子,请参考官方的Android培训页面:
或
$ C $的CProject:
I'm quite new to android programming and getting an error I couldn't figure out. I basicly wrote a code that let the user send his current coordinates to a local SQLite database. this crush happens when I'm starting to insert the data into the dataabse.I keep getting-
android.database.sqlite.SQLiteException: near "CREATE_RECORD_TABLE": syntax error (code 1): , while compiling: CREATE_RECORD_TABLE ( id INTEGER PRIMARY KEY AUTOINCREMENT, latitude TEXT, longtitude TEXT, accuracy TEXT, time TEXT )
And i'm pretty sure it happens due alot of confusion I have with the different types. I'll be happy if you'll be able to take a look and pinpoint me to the right direction.
the record (table) activity :
package com.program.android.taskir;
public class Record {
//private variables
private int id;
private double pLong;
private double pLat;
private float pAcc;
private long pTime;
public Record(){}
// Empty constructor
// constructor
public Record( double pLong, double pLat, float pAcc, long pTime){
super();
this.pLong = pLong;
this.pLat= pLat;
this.pAcc= pAcc;
this.pTime= pTime;
}
@Override
public String toString() {
return "Record [id=" + id + ", Longtitude=" + pLong + ", Latitude=" + pLat + ", Accuracy" + pAcc + ", Time" +pTime
+ "]";
}
// getting ID
public int getID(){
return this.id;
}
// setting id
public void setID(int id){
this.id = id;
}
// getting pLong
public double getpLong(){
return this.pLong;
}
// setting pLong
public void setpLong(double pLong){
this.pLong = pLong;
}
// getting pLat
public double getpLat(){
return this.pLat;
}
// setting pLat
public void setpLat(double pLat){
this.pLat = pLat;
}
// getting pAcc
public float getpAcc(){
return this.pAcc;
}
// setting pAcc
public void setpAcc(float pAcc){
this.pAcc = pAcc;
}
// getting pTime
public long getpTime(){
return this.pTime;
}
// setting pTime
public void setpTime(long pTime){
this.pTime = pTime;
}
}
the SQlite activity :
package com.program.android.taskir;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.LinkedList;
import java.util.List;
public class MySQLiteHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "RecordsDB";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create record table
String CREATE_RECORD_TABLE = "CREATE_RECORD_TABLE ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"latitude TEXT, "+
"longtitude TEXT, "+
"accuracy TEXT, "+
"time TEXT )";
// create books table
db.execSQL(CREATE_RECORD_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older books table if existed
db.execSQL("DROP TABLE IF EXISTS Records");
// create fresh record table
this.onCreate(db);
}
// Books table name
private static final String TABLE_RECORD = "record";
// Books Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_LONG = "longtitude";
private static final String KEY_LAT = "latitude";
private static final String KEY_ACC = "accuracy";
private static final String KEY_TIME = "time";
private static final String[] COLUMNS = {KEY_ID,KEY_LONG,KEY_LAT,KEY_ACC,KEY_TIME};
public void addRecord(Record record){
//for logging
Log.d("addBook", record.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_LONG, record.getpLong());
values.put(KEY_LAT, record.getpLat());
values.put(KEY_ACC, record.getpAcc());
values.put(KEY_TIME, record.getpTime());
// 3. insert
db.insert(TABLE_RECORD, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
public Record getRecord(int id){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_RECORD, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String[] { String.valueOf(id) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
// 4. build book object
Record record = new Record();
record.setID(Integer.parseInt(cursor.getString(0)));
record.setpLat(cursor.getDouble(1));
record.setpLong(cursor.getDouble(2));
record.setpAcc(cursor.getFloat(2));
record.setpTime(cursor.getLong(2));
//log
Log.d("getBook("+id+")", record.toString());
// 5. return book
return record;
}
public List<Record> getAllRecords() {
List<Record> records = new LinkedList<Record>();
// 1. build the query
String query = "SELECT * FROM " + TABLE_RECORD;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build book and add it to list
Record record = null;
if (cursor.moveToFirst()) {
do {
record = new Record();
record.setID(Integer.parseInt(cursor.getString(0)));
record.setpLat(cursor.getDouble(1));
record.setpLong(cursor.getDouble(2));
record.setpAcc(cursor.getFloat(2));
record.setpTime(cursor.getLong(2));
// Add book to books
records.add(record);
} while (cursor.moveToNext());
}
Log.d("getAllRecords()", record.toString());
// return books
return records;
}
public int UpdateRecords(Record record) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("Latitude", record.getpLat()); //
values.put("Longtitude", record.getpLong());
values.put("Accuracy", record.getpAcc());
values.put("Time", record.getpTime());
// 3. updating row
int i = db.update(TABLE_RECORD, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(record.getID()) }); //selection args
// 4. close
db.close();
return i;
}
public void deleteRecords(Record record) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. delete
db.delete(TABLE_RECORD, //table name
KEY_ID+" = ?", // selections
new String[] { String.valueOf(record.getID()) }); //selections args
// 3. close
db.close();
//log
Log.d("deleteBook", record.toString());
}
}
The main activity :
package com.program.android.taskir;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class GpsPage extends ActionBarActivity {
TextView tz;
TextView textLat;
TextView textLong;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps_page);
final MySQLiteHelper db = new MySQLiteHelper(this);
tz = (TextView) findViewById(R.id.textviewtz);
Intent intent = getIntent();
String TeudatZeut = intent.getStringExtra("tz");
tz.setText("תעודת זהות " + TeudatZeut);
Switch onOffSwitch = (Switch) findViewById(R.id.switch1);
onOffSwitch.setChecked(false);
onOffSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getApplicationContext(), "שולח נתונים לשרת", Toast.LENGTH_SHORT).show();
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
double pLong = location.getLongitude();
double pLat = location.getLatitude();
float pAcc = location.getAccuracy();
long pTime = location.getTime();
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
db.addRecord(new Record(pLong, pLat, pAcc, pTime));
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
}
);
/**
* CRUD Operations
* */
//add record
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_gps_page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat report -
12-29 12:10:29.400: D/addBook(894): Record [id=0, Longtitude=1.0, Latitude=3.0, Accuracy20.0, Time1419847204000]
12-29 12:10:29.470: D/addBook(894): Record [id=0, Longtitude=1.0, Latitude=3.0, Accuracy20.0, Time1419847204000]
12-29 12:10:29.510: D/addBook(894): Record [id=0, Longtitude=1.0, Latitude=3.0, Accuracy20.0, Time1419847204000]
12-29 12:10:29.540: D/dalvikvm(894): GC_FOR_ALLOC freed 112K, 9% free 4333K/4728K, paused 28ms, total 34ms
12-29 12:10:29.580: D/addBook(894): Record [id=0, Longtitude=1.0, Latitude=3.0, Accuracy20.0, Time1419847204000]
12-29 12:10:29.620: D/addBook(894): Record [id=0, Longtitude=1.0, Latitude=3.0, Accuracy20.0, Time1419847204000]
12-29 12:10:33.860: D/dalvikvm(385): GC_FOR_ALLOC freed 761K, 21% free 6394K/8004K, paused 62ms, total 70ms
The mistake you made is in the SQlite
Activity's onCreate(SQLiteDatabase db)
method.Your database query should look like below :
String CREATE_RECORD_TABLE = "CREATE TABLE RECORD ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"latitude TEXT NOT NULL, "+
"longtitude TEXT NOT NULL, "+
"accuracy TEXT NOT NULL, "+
"time TEXT NOT NULL )";
For a full fledged example on Databases , refer Android official training page :http://developer.android.com/training/notepad/index.html
ORCodeproject :http://www.codeproject.com/Articles/524204/Simple-notepad-for-Android
这篇关于android.database.sqlite.SQLiteException:临近&QUOT; CREATE_RECORD_TABLE&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!