我是android的新手,并试图学习如何创建和插入数据库的东西。我设法创建它并且app运行但是每当我尝试插入东西时我都会收到此错误:
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;
}
}
答案 0 :(得分:3)
public static final String KEY_TRIP_TYPE = "trip type";
而不是
public static final String KEY_TRIP_TYPE = "trip_type";
因为sqlite中的数据库字段中不允许使用空格。
如果你已经在资产db文件夹中创建了数据库表,那么不要在java文件中创建它。如果你没有在外部创建表,那么使用它:
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);
如果这个ans对您有用,请投票。
答案 1 :(得分:1)
您的数据库列名称中似乎有空格,而是使用“_”:
public static final String KEY_TRIP_TYPE = "trip_type";
答案 2 :(得分:0)
您的列名称行程类型之间有空格。这意味着你将不得不使用它。例如。 `旅行类型。或者更好的是,用下划线替换空格而不是空格。
E.g。 public static final String KEY_TRIP_TYPE = "trip_type";
您也可以使用其他列进行此操作。
注意:由于您更改了列名称,因此可能有toclear数据,或者卸载然后重新安装应用程序。
答案 3 :(得分:0)
改变这个:
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";
了解更多内容here。此外,您还需要使用数据库版本升级来更新表结构。