将行插入sqLite数据库时出错

时间:2016-12-15 04:31:01

标签: android database sqlite

所以这是我的第一个sqlite数据库,我遇到了一个试图将行插入数据库的问题。我想要做的就是将整数插入到sqlite数据库中。也许这比我正在做的更简单?我可以使用CheckedRoutes类吗?

Heres myDbHandler

https://twitter.com/{{#twitterUserName}}/profile_image?size=original

继承我的CheckedRoutes

package com.example.zach.listview;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.content.ContentValues;

public class MyDBHandler extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "checked_routes.db";
public static final String TABLE_CHECKED_ROUTES = "checked_routes";
public static final String COLUMN_ID = "_checkedRoutes";

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String query = "CREATE TABLE " + TABLE_CHECKED_ROUTES +
            "(" + COLUMN_ID + " INTEGER PRIMARY KEY " + ");";
    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHECKED_ROUTES);
    onCreate(db);
}


/////Add row to table
public void addCheckedRoute(CheckedRoutes checkedRoute){
    ContentValues values = new ContentValues();
    values.put(COLUMN_ID, checkedRoute.get_checkedRoute());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_CHECKED_ROUTES, null, values);
    db.close();
}

////Delete row from table
public void deleteCheckedRow(int checkedRoute){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_CHECKED_ROUTES + "WHERE " + COLUMN_ID + "=\"" + checkedRoute + "\";" );
}
}

而且我是如何尝试插入行的

package com.example.zach.listview;


public class CheckedRoutes {

int _checkedRoute;

   public void set_checkedRoute(int _checkedRoute){
    this._checkedRoute = _checkedRoute;
   }

public int get_checkedRoute(){
    return _checkedRoute;
}
}

并且是我的错误

MyDBHandler dbHandler;
int listViewItemPosition;
listViewItemPosition = getIntent().getIntExtra("listViewItemPosition", 0);

 //add checked route position to database
 CheckedRoutes checkedRoute = new CheckedRoutes();
 checkedRoute.set_checkedRoute(listViewItemPosition);
 dbHandler.addCheckedRoute(checkedRoute);

感谢您的帮助

0 个答案:

没有答案