我有问题。 我使用了androidhive的sqlite数据库教程 (http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/)
我试图添加一个字段。但是当我这样做时,我得到以下例外:
03-26 17:32:37.770: E/SQLiteLog(29021): (1) near "where": syntax error
03-26 17:32:37.770: W/dalvikvm(29021): threadid=1: thread exiting with uncaught exception (group=0x416c1e18)
03-26 17:32:37.770: E/AndroidRuntime(29021): FATAL EXCEPTION: main
03-26 17:32:37.770: E/AndroidRuntime(29021): Process: com.androidhive.androidsqlite, PID: 29021
03-26 17:32:37.770: E/AndroidRuntime(29021): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.androidsqlite/com.androidhive.androidsqlite.AndroidSQLiteTutorialActivity}: android.database.sqlite.SQLiteException: near "where": syntax error (code 1): , while compiling: CREATE TABLE vm(id INTEGER PRIMARY KEY,date TEXT,what TEXT,where TEXT)
03-26 17:32:37.770: E/AndroidRuntime(29021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
以下是代码:
AndoidSQLiteTutorialActivity
package com.androidhive.androidsqlite;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class AndroidSQLiteTutorialActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addAppointment(new Appointment("Test date", "Test what", "Test where"));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Appointment> appointments = db.getAllAppointments();
for (Appointment appointment : appointments) {
String log = "Id: "+appointment.getID()+" ,What: " + appointment.getWhat() + " ,Where: " + appointment.getWhere();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
}
数据库处理器
package com.androidhive.androidsqlite;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "appointmentsManager";
//table name
private static final String TABLE_VM = "vm";
//Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_DATE = "date";
private static final String KEY_WHAT = "what";
private static final String KEY_WHERE = "where";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Table
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_VM_TABLE = "CREATE TABLE " + TABLE_VM + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
+ KEY_WHAT + " TEXT," + KEY_WHERE + " TEXT" + ")";
db.execSQL(CREATE_VM_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_VM);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new appointment
void addAppointment(Appointment appointment) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DATE, appointment.getDate());
values.put(KEY_WHAT, appointment.getWhat());
values.put(KEY_WHERE, appointment.getWhere());
// Inserting Row
db.insert(TABLE_VM, null, values);
db.close(); // Closing database connection
}
// Getting single appointment
Appointment getAppointment(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_VM, new String[] { KEY_ID,
KEY_DATE, KEY_WHAT, KEY_WHERE}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Appointment appointment = new Appointment(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3));
// return contact
return appointment;
}
// Getting All appointments
public List<Appointment> getAllAppointments() {
List<Appointment> appointmentList = new ArrayList<Appointment>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_VM;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Appointment appointment = new Appointment();
appointment.setID(Integer.parseInt(cursor.getString(0)));
appointment.setDate(cursor.getString(1));
appointment.setWhat(cursor.getString(2));
appointment.setWhere(cursor.getString(3));
// Adding appintment to list
appointmentList.add(appointment);
} while (cursor.moveToNext());
}
// return appointment list
return appointmentList;
}
// Updating single appointment
public int updateAppointment(Appointment appointment) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DATE, appointment.getDate());
values.put(KEY_WHAT, appointment.getWhat());
values.put(KEY_WHERE, appointment.getWhere());
// updating row
return db.update(TABLE_VM, values, KEY_ID + " = ?",
new String[] { String.valueOf(appointment.getID()) });
}
// Deleting single appointment
public void deleteAppointment(Appointment contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_VM, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// Getting appointments Count
public int getAppointmentsCount() {
String countQuery = "SELECT * FROM " + TABLE_VM;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
预约
package com.androidhive.androidsqlite;
public class Appointment {
//private variables
int id;
String date;
String what;
String where;
/**
* Constructs an empty appointment
*/
public Appointment(){
}
// constructor
public Appointment(String date, String what, String where){
this.date = date;
this.what = what;
this.where = where;
}
// constructor
public Appointment(int id, String date, String what, String where){
this.id = id;
this.date = date;
this.what = what;
this.where = where;
}
// getting ID
public int getID(){
return this.id;
}
// setting id
public void setID(int id){
this.id = id;
}
// getting phone number
public String getDate(){
return this.date;
}
// setting phone number
public void setDate(String date){
this.date = date;
}
// getting name
public String getWhat(){
return this.what;
}
// setting name
public void setWhat(String what){
this.what = what;
}
// getting name
public String getWhere(){
return this.where;
}
// setting name
public void setWhere(String where){
this.where = where;
}
}
感谢您的帮助。
问候
答案 0 :(得分:2)
where
是SQLite中的保留字(以及使用SQL-92兼容语言的所有数据库引擎)。
它根本不能用作列名。
您可以使用类似的字符串替换它,例如key_where
。