数据未存储在Sqlite表中

时间:2011-07-14 13:30:57

标签: android sqlite

任何人都可以在这里查看我的代码。我试图使用onClick方法存储我的主要布局中的一些数据。这里是我的onClick方法的代码

private OnClickListener bNextListener = new OnClickListener() {
    public void onClick(View v) {
        name=txt1.getText().toString();
        email=txt2.getText().toString();
            date = txtv.getText().toString();
            gender = b4.getText().toString();
        dbHelper.addProfiles(name, date, email, gender);
};

每当我运行我的应用程序时,它都会崩溃,因为它无法将数据存储在我的dbHelper类中的addProfiles方法中。这是我的dbHelper类 公共类SqlHelper扩展SQLiteOpenHelper {     public static final String DATABASE_PATH =“/ data / data /com.and.profile / database /”;     public static final String DATABASE_NAME =“profiledatabase.db”;     private static final int DATABASE_VERSION = 2;

public static final String PROFILES_TABLE = "profiles";
public static final String COLUMN_PROFILE_ID = "profile_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_BIRTHDAY = "birthday";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_GENDER = "gender";

public static final String INTERESTS_TABLE = "interests";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_SELECTED = "selected";

private static final String CREATE_TABLE_1 =
        " create table " + PROFILES_TABLE +
        " (" + COLUMN_PROFILE_ID + " integer primary key autoincrement," +
               COLUMN_NAME + " name text not null, "+ 
               COLUMN_BIRTHDAY +" birthday date not null, " + 
               COLUMN_EMAIL + "email text not null, " + 
               COLUMN_GENDER +" gender text not null);";

private static final String CREATE_TABLE_2 =
        " create table " + INTERESTS_TABLE +
        " (" + COLUMN_ID + " integer primary key autoincrement," +
           COLUMN_TITLE + " name text not null, "+ 
           COLUMN_SELECTED +" selected integer);";

public SQLiteDatabase dbSqlite;
//private boolean dbExist = true;
private final Context myContext;

public SqlHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // check if exists and copy database from resource
    //createDB();
db.execSQL(CREATE_TABLE_1);
db.execSQL(CREATE_TABLE_2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w("SqlHelper", "Upgrading database from version " + oldVersion
            + " to " + newVersion + ", which will destroy all old data");
    onCreate(db);
}

public void createDataBase() throws IOException{

    boolean dbExist = CheckDataBase();

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
           //of your application so we are gonna be able to overwrite that database with our database.
        this.getWritableDatabase();

        copyDBFromResource();
    }

}

boolean CheckDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String databasePath = DATABASE_PATH + DATABASE_NAME;
        checkDB = SQLiteDatabase.openDatabase(databasePath, null,
                SQLiteDatabase.OPEN_READWRITE);
        checkDB.setLocale(Locale.getDefault());
        checkDB.setLockingEnabled(true);
        checkDB.setVersion(1);
    } catch (SQLiteException e) {
        Log.e("SqlHelper", "database not found");
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDBFromResource() {
    InputStream inputStream = null;
    OutputStream outStream = null;
    String dbFilePath = DATABASE_PATH + DATABASE_NAME;
    try {
        inputStream = myContext.getAssets().open(DATABASE_NAME);
        outStream = new FileOutputStream(dbFilePath);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }
        outStream.flush();
        outStream.close();
        inputStream.close();
    } catch (IOException e) {
        throw new Error("Problem copying database from resource file.");
    }
}

public void openDataBase() throws SQLException {
    String myPath = DATABASE_PATH + DATABASE_NAME;
    dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}

@Override
public synchronized void close() {
    if (dbSqlite != null)
        dbSqlite.close();
        super.close();
}

public void addProfiles( String name, String date, String email, String gender) {
    System.out.println("addProfiles");
    System.out.println("entre2");
    dbSqlite.execSQL("INSERT INTO " + PROFILES_TABLE + " ('name','date','email','gender') values ('" +
          COLUMN_NAME + "', '" + COLUMN_BIRTHDAY + "', '" + COLUMN_EMAIL + "', '" + COLUMN_GENDER + "')");

      }

}

如果专家提供一些快速帮助,我将非常高兴。顺便说一下,如果需要,我在资源文件夹中使用我的数据库文件。

2 个答案:

答案 0 :(得分:0)

这需要经过很多代码,但只看最后一个方法:

dbSqlite.execSQL("INSERT INTO " + PROFILES_TABLE + " ('name','date','email','gender') values ('" +
          COLUMN_NAME + "', '" + COLUMN_BIRTHDAY + "', '" + COLUMN_EMAIL + "', '" + COLUMN_GENDER + "')");

这没有多大意义:

  • 您没有使用所有传入的变量
  • 您正在硬编码(并且引用 - 不必要)字符串
  • 中的列名称
  • 您使用常量作为插入值
  • 之后您没有进行任何错误检查。

清理它,并确保打印出任何错误消息。

答案 1 :(得分:0)

据我所知,在addProfiles中,你的语句是向后的,虽然我认为你想访问传递给方法的参数,但实际上你只是将它们作为字符串访问。它应该是Insert Into(Database.Table) - > (列) - >值。真的,你不应该需要这些列。