2

我正在尝试在 android 的 sqllite 数据库中插入数据。

我的适配器类

public class dbAdapter {
    // Database fields
    public static final String ID = "id";
    public static final String FULLNAME = "fullname";
    public static final String ADDRESS = "address";
    public static final String EMAIL = "email";
    public static final String MOBILE = "mobile";
    public static final String GENDER = "gender";
    public static final String DOB = "dob";
    public static final String CITY = "city";
    public static final String USERNAME = "username";
    public static final String PASSWORD = "password";

    private static final String DATABASE_TABLE = "myfirstanroidapp";

    private Context context;
    private SQLiteDatabase database;
    private databaseHelper dbHelper;

    public dbAdapter(OnClickListener onClickListener) {
        this.context = (Context) onClickListener;
        open();
    }

    public dbAdapter(Context context) {
        Log.e("Adapter","dbAdapter context");
        this.context = context;
    }

    public dbAdapter open() throws SQLException {

        dbHelper = new databaseHelper(context);
        Log.e("Adapter","getWritableDatabase");
        database = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        dbHelper.close();
    }

    // Put String values into a Content object(like AssignData to database field)
    private ContentValues createContentValues(String fullname, String address, String email,String mobile, String gender, String dob,String city, String username, String password) {
        ContentValues values = new ContentValues();
        values.put(FULLNAME, fullname);
        values.put(ADDRESS, address);
        values.put(EMAIL, email);
        values.put(MOBILE, mobile);
        values.put(GENDER, gender);
        values.put(DOB, dob);
        values.put(CITY, city);
        values.put(USERNAME, username);
        values.put(PASSWORD, password);
        return values;
    }

    // Create a new myfirstanroidapp. If the myfirstanroidapp is successfully created return the new
    // rowId for that note, otherwise return a -1 to indicate failure.
    public long insert(String fullname, String address, String email,String mobile, String gender, String dob,String city, String username, String password) {  
        ContentValues assigndata = createContentValues(fullname,address,email,mobile,gender,dob,city,username, password) ;  
        return database.insert(DATABASE_TABLE, null, assigndata);
    }
}

我的助手类

public class databaseHelper extends SQLiteOpenHelper 
{
    private static final String DATABASE_NAME = "myfirstanroidapp";
    private static final int DATABASE_VERSION = 1;
    // Database creation SQL statement
    private static final String DATABASE_CREATE = 
            "create table myfirstanroidapp (id integer primary key autoincrement, " +
            "fullname text not null, address text, " +
            "mobile text not null, email text not null," +
            "city text not null, username text not null, " +
            "password text not null, dob text not null," +
            "gender text not null);";


    public databaseHelper(Context context){     
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.e("Database",DATABASE_NAME);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create Database
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Drop Database and ReCreate Database
        db.execSQL("DROP TABLE IF EXISTS myfirstanroidapp");
        onCreate(db);   
    }
}

现在我从 onButtonClick 传递数据

dbAdapter = new dbAdapter(getApplicationContext());
dbAdapter.insert("XYZ", "XYZ", "xyz.gmail.com", "123456789", "male", "dob", "pune", "username", "password");
Log.e("Sucess","Register");

但出现错误 在此处输入图像描述

4

1 回答 1

1

改变

public dbAdapter(OnClickListener onClickListener) {
        this.context = (Context) onClickListener;
        open();
    }

dbAdapter = new dbAdapter(this);

public dbAdapter(Context context) {
        this.context = context;
        open();
    }

dbAdapter = new dbAdapter(Your_activity.this);

于 2012-12-03T05:45:06.453 回答