尝试插入值时出现 SQLite 错误 [英] SQLite error when trying to Insert Values

查看:44
本文介绍了尝试插入值时出现 SQLite 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android 新手,正在尝试学习如何创建内容并将其插入到数据库中.我设法创建了它并且应用程序运行,但是每当我尝试插入内容时都会收到此错误:

I'm new to android and trying to learn how to create and insert stuff into a database. I managed to create it and the app runs but I get this error whenever I try to insert stuff in:

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 活动数据库部分:

This is my PlanTrip activity database part:

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:

And my 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;
    }
} 

推荐答案

您的 db 列名称中似乎有空格,请改用_":

It seems you have spaces in your db column names, use '_' instead:

public static final String KEY_TRIP_TYPE = "trip_type";

这篇关于尝试插入值时出现 SQLite 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆