我正在尝试获取信息并将其设置到我的数据库中,但它一直返回 null。
我测试了它,我的 DatabaseHelper
类以及我如何使用它似乎有问题。这是我的 DatabaseHelper
类。数据库本身已经使用 sqlite 数据库管理器创建并位于 Assets 文件夹中。
public class DatabaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/package_name/databases/";
private static String DB_NAME = "example.db";
private static int DB_VERSION = 1;
private SQLiteDatabase myDataBase;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
}else{
with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public void setStuff (String columns, String table, String values, String
whereclause){
openDataBase();
myDataBase.execSQL("INSERT INTO "
+ table
+ " "+columns
+ " VALUES ('"+values+")" + " WHERE "+whereclause);
close();
}
public ArrayList<String> getStuff (String column, String table, String whereclause){
ArrayList<String> stuffList = new ArrayList<String>();
openDataBase();
Cursor c = myDataBase.rawQuery("SELECT "+column +
" FROM " +table
+ " WHERE "+whereclause,
null);
int index = c.getColumnIndex(column);
if (c != null) {
if (c.isFirst()) {
do {
String stuff = c.getString(index).toString();
results.add(data);
} while (c.getString(index)!=null);
}
}
close();
return stuffList;
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
接下来我实际尝试使用这个数据库助手的类是我的 PersonDBController
类,它在我的 signUp Activity 中使用,我只是调用带有参数的方法。
public class PersonDBController {
private DatabaseHelper d;
private Person p;
public String getFirstname(int userIDnum) {
ArrayList<String> fn =
d.getStuff("firstname",
"people", "userIDnum
="+userIDnum);
String result = fn.get(0);
return result;
}
这只是该类中的众多方法之一。但是,这并不能正常完成工作。当在 Activity 中调用时——比如在按钮上单击以设置或获取内容,它会返回一个空指针异常。
知道我做错了什么吗?
最佳答案
您可能永远不会初始化d
。检查你的代码,里面有没有类似下面的东西?
d = new DatabaseHelper(this);
如果不是,那可能就是您的问题所在。
关于java - android sqlite数据库,空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8114794/