我是 android 新手,正在尝试学习如何创建并将内容插入数据库。我设法创建它并且应用程序运行但每当我尝试在其中插入东西时都会收到此错误:
Error inserting trip type=Hi title=Beach destination=Gotham
android.database.sqlite.SQLiteException near "type": synthax error(code 1):, while
compiling: INSERT INTO trips(trip type, title, destination) VALUES (?,?,?)
这是我的 PlanTrip 活动数据库部分:
private EditText editText1;
private EditText editText2;
private EditText editText3;
DBAdapter db = new DBAdapter(this);
try {
String destPath = "/data/data/" + getPackageName() + "/databases/Trips.db";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("db"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
EditText titleTxt = (EditText)findViewById(R.id.editText1);
EditText destinationTxt = (EditText)findViewById(R.id.editText2);
EditText tripTypeTxt = (EditText)findViewById(R.id.editText3);
db.open();
long id = db.insertRecord(titleTxt.getText().toString(),
destinationTxt.getText().toString(), tripTypeTxt.getText().toString());
db.close();
还有我的 DBAdapter:
public class DBAdapter {
public static final String KEY_TRIPID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_DESTINATION = "destination";
public static final String KEY_TRIP_TYPE = "trip type";
public static final String KEY_STARTDATE = "start date";
public static final String KEY_ENDDATE = "end date";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "Trips.db";
private static final String DATABASE_TABLE = "trips";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
/*private static final String DATABASE_CREATE =
"create table if not exists trips (trip_id integer primary key autoincrement, "
+ "title VARCHAR not null, destination VARCHAR not null, triptype VARCHAR not null,"
+ " startdate VARCHAR, enddate VARCHAR);";*/
private static final String DATABASE_CREATE =
"CREATE table if not exists " + DATABASE_TABLE + " (" + KEY_TRIPID
+ " integer PRIMARY KEY autoincrement," + KEY_TITLE + ","
+ KEY_DESTINATION + ", " + KEY_TRIP_TYPE + ");";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion +
" to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS trips" );
onCreate(db);
}
}
//open db
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//close db
public void close()
{
DBHelper.close();
}
//insert record into db
public long insertRecord(String title, String destination, String tripType)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_DESTINATION, destination);
initialValues.put(KEY_TRIP_TYPE, tripType);
Toast.makeText(context, "Record Added", Toast.LENGTH_SHORT).show();
return db.insert(DATABASE_TABLE, null, initialValues);
}
//delete record
public boolean deleteRecord(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_TRIPID + "=" + rowId, null) > 0;
}
//get all records
public Cursor getAllRecords()
{
return db.query(DATABASE_TABLE, new String[] {KEY_TRIPID, KEY_TITLE, KEY_DESTINATION, KEY_TRIP_TYPE},
null, null, null, null, null);
}
//get specific record
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_TRIPID, KEY_TITLE, KEY_DESTINATION, KEY_TRIP_TYPE},
KEY_TRIPID + "=" + rowId, null, null, null, null, null);
if (mCursor != null)
mCursor.moveToFirst();
return mCursor;
}
//update record
public boolean UpdateRecord (long rowId, String title, String destination, String tripType)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_DESTINATION, destination);
args.put(KEY_TRIP_TYPE, tripType);
return db.update(DATABASE_TABLE, args, KEY_TRIPID + "=" + rowId, null) > 0;
}
}