带有多个表的Android SQLite

时间:2016-02-17 19:43:55

标签: android sqlite

public class DatabaseHandler extends SQLiteOpenHelper {

   //COLUMNS OF THE NEW USER TABLE
    public static final String TABLE_NEWUSER = "newUser";
    public static final String COLUMN_NEWUSER_ID = "id";
    public static final String COLUMN_NEWUSER_NAME = "name";
    public static final String COLUMN_NEW_USER_PASSWORD = "password";
    public static final String COLUMN_NEW_USER_AGE = "age";

    //COLUMNS OF THE BALANCE TABLE
    public static final String COLUMN_BALANCE_ID = "id";
    public static final String TABLE_BALANCE = "balanceOfUser";
    public static final String COLUMN_BALANCE_DOLLARBALANCE = "dollarBalance";
    public static final String COLUMN_BALANCE_RUBBALANCE = "rubBalance";
    public static final String COLUMN_BALANCE_NEW_USER_ID = "newUserId";

    private static final String DATABASE_NAME = "webStore";
    private static final int DATABASE_VERSION = 1;

    private static final String SQL_CREATE_NEWUSER = "CREATE TABLE " + TABLE_NEWUSER + "("
            + COLUMN_NEWUSER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_NEWUSER_NAME + " TEXT NOT NULL, "
            + COLUMN_NEW_USER_PASSWORD + " TEXT NOT NULL, "
            + COLUMN_NEW_USER_AGE + " INTEGER NOT NULL"
            + ");";
    private static final String SQL_CREATE_BALANCE = "CREATE TABLE" + TABLE_BALANCE + "("
            + COLUMN_BALANCE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_BALANCE_DOLLARBALANCE + " INTEGER NOT NULL"
            + COLUMN_BALANCE_RUBBALANCE + " INTEGER NOT NULL"
            + COLUMN_BALANCE_NEW_USER_ID + " INTEGER NOT NULL"
            + ");";
    public DatabaseHandler(Context context){
        super(context,DATABASE_NAME,null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_BALANCE);
        db.execSQL(SQL_CREATE_NEWUSER);
        onCreate(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BALANCE);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEWUSER);
    }
  public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,int version){
      super(context,DATABASE_NAME,factory,DATABASE_VERSION);
  }

}

public void CreateUser(View view) {
    etName = (EditText)findViewById(R.id.etName);
    etPassword = (EditText)findViewById(R.id.etPassword);
    etAge = (EditText)findViewById(R.id.etAge);
    String name = String.valueOf(etName.getText());
    String password = String.valueOf(etPassword.getText());
    int age = Integer.parseInt(String.valueOf(etAge.getText()));
    //write to database of user from our edit texts
    DatabaseHandler databaseHandler = new DatabaseHandler(this);
    Log.d("Insert: ", "Inserting ..");
    NewUserDAO dbForUser = new NewUserDAO(this);
    dbForUser.createNewUser(name,password,age);
}

我有2个表,在表格中我有用户的密钥。当我创建新用户(createUser方法)时,我有例外:

  

引起:java.lang.NullPointerException:尝试调用虚方法' long android.database.sqlite.SQLiteDatabase.insert(java.lang.String,java.lang.String,android.content.ContentValues) '在null对象引用上。

问题出在哪里?感谢

public class NewUserDAO {
public static final String TAG = "NewUserDAO";

//database fields
private SQLiteDatabase mDataBase;
private DatabaseHandler mDbHelper;
private Context mContext;
private String [] mAllColumns = {
        DatabaseHandler.COLUMN_NEWUSER_ID,
        DatabaseHandler.COLUMN_NEWUSER_NAME, DatabaseHandler.COLUMN_NEW_USER_PASSWORD,
        DatabaseHandler.COLUMN_NEW_USER_AGE };

public NewUserDAO(Context context){
    this.mContext = context;
    mDbHelper = new DatabaseHandler(context);
    try{
        open();
    } catch (SQLException e){
        Log.e(TAG,"SQLexception on opening database" + e.getMessage());
        e.printStackTrace();
    }
}
public void open() throws SQLException{
    mDataBase = mDbHelper.getWritableDatabase();
}
public void close(){
    mDbHelper.close();
}
public NewUserTable createNewUser(String name, String password, int age){
    ContentValues values = new ContentValues();
    values.put(DatabaseHandler.COLUMN_NEWUSER_NAME,name);
    values.put(DatabaseHandler.COLUMN_NEW_USER_PASSWORD,password);
    values.put(DatabaseHandler.COLUMN_NEW_USER_AGE, age);
    long insertId = mDataBase.insert(DatabaseHandler.TABLE_NEWUSER, null, values);
    Cursor cursor = mDataBase.query(DatabaseHandler.TABLE_NEWUSER, mAllColumns,
            DatabaseHandler.COLUMN_NEWUSER_ID + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    NewUserTable newUser = cursorToUser(cursor);
    cursor.close();
    return newUser;
}

public List<NewUserTable> getAllUsers(){
    List<NewUserTable> listUsers = new ArrayList<>();
    Cursor cursor = mDataBase.query(DatabaseHandler.TABLE_NEWUSER, mAllColumns, null,null,null,null,null);
    if (cursor != null) {
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            NewUserTable nut = cursorToUser(cursor);
            listUsers.add(nut);
            cursor.moveToNext();
        }
        cursor.close();
    }
    return listUsers;
}
public NewUserTable getUserById (long id){
    Cursor cursor = mDataBase.query(DatabaseHandler.TABLE_NEWUSER, mAllColumns, DatabaseHandler.COLUMN_NEWUSER_ID + " = ?",
            new String[]{String.valueOf(id)}, null, null, null );
    if ( cursor !=  null){
        cursor.moveToFirst();
    }
    NewUserTable nut = cursorToUser(cursor);
    return nut;
}
protected NewUserTable cursorToUser (Cursor cursor){
    NewUserTable nut = new NewUserTable();
    nut.setID(cursor.getLong(0));
    nut.setName(cursor.getString(1));
    nut.setPassword(cursor.getString(2));
    nut.setAge(cursor.getInt(4));
    return nut;
}

}

2 个答案:

答案 0 :(得分:0)

您正在获取edittext值,而不存储任何内容。所以字符串名称存储空值。之后,您在查询中使用它。此处发生异常,因为name为null,password和age为null。

答案 1 :(得分:0)

尝试在创建表架构时删除@+。你是如何处理NOT NULL方法的?