【发布时间】:2023-04-02 01:25:01
【问题描述】:
我正在尝试获取信息并将其设置到我的数据库中,但它一直返回 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 类,它又在我的注册活动中使用,我只需使用参数调用该方法。
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;
}
这只是该类中众多方法之一。但是,这并不能正确地完成工作。当在活动中调用时 - 例如单击按钮以设置或获取内容,它会返回空指针异常。
知道我做错了什么吗?
【问题讨论】:
-
您需要将其归结为 SSCCE 来演示问题并将其发布在此处。值得注意的是,这样做时,您经常会发现自己的错误。把你所有的代码都放在这里然后问“怎么了”通常不会有太多帮助。
-
如果你得到一个空指针异常,那么你应该给我们堆栈跟踪。这样可以更轻松地找到问题,而无需查看所有代码。
-
例如有“我们的数据库”。在语法之外的代码中
-
代码中这是什么?! -> " 使用我们的数据库。this.getReadableDatabase();"
-
基本上使用 LogCat 来跟踪哪些方法失败了,这将是一个很好的起点
标签: java android database sqlite nullpointerexception