我在sqlite studio中创建了一个数据库,我将其导入到我的Android应用程序中。 SQLite帮助程序代码如下:
public class DatabaseHelper extends SQLiteOpenHelper {
String DB_PATH = null;
private static String DB_NAME = "mydatabases";
private static SQLiteDatabase myDataBase;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 21);
this.myContext = context;
this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
Log.e("Path 1", DB_PATH);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[10];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
catch(IOException ex){}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion){
try {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[10];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在我想从数据库中获取数据,下面是在活动加载中检索数据的代码:
onActivityCreated代码:
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
final ArrayList<String> strArary;
myDbHelper = new DatabaseHelper(getActivity());
c = myDbHelper.getReadableDatabase().rawQuery("SELECT * FROM categories",null) ; // myDbHelper.query("categories",null,null,null,null,null,null);
if (c.moveToFirst()) {
do {
strArary1.add(c.getString(0));
strArary.add(c.getString(1));
} while (c.moveToNext());
}
鉴于代码提出了一些错误,如问题标题所述。
请帮我解决问题。
答案 0 :(得分:-1)
这是我的课,对我来说很好用:
public class DBHandler extends SQLiteOpenHelper {
private static final String DataBase_Name = "yourdb";
private static final int DataBase_Version = 3;
private static final String Path ="/data/data/" + context.getPackageName() + "/" + "databases/";
public Cursor cursor;
protected Context context;
SQLiteDatabase DB;
String url;
public DBHandler(Context context) {
super(context, DataBase_Name, null, DataBase_Version);
this.context = context;
try {
OpenDataBase();
Close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void CopyDataBase() throws IOException {
InputStream myinput = context.getAssets().open(DataBase_Name);
String output = Path + DataBase_Name;
OutputStream myoutputstream = new FileOutputStream(output);
byte[] buffer = new byte[1024];
int length;
try {
while ((length = myinput.read(buffer)) >= 0) {
myoutputstream.write(buffer, 0, length);
}
myoutputstream.flush();
myoutputstream.close();
myinput.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
protected boolean ChekDB() {
SQLiteDatabase exist = null;
try {
String DBPath = Path + DataBase_Name;
exist = SQLiteDatabase.openDatabase(DBPath, null, SQLiteDatabase.OPEN_READWRITE);
if (DataBase_Version > exist.getVersion()) {
exist.close();
CopyDataBase();
exist = SQLiteDatabase.openDatabase(DBPath, null, SQLiteDatabase.OPEN_READWRITE);
exist.setVersion(DataBase_Version);
Util.saveDataToShared(context, "sync", "fixedInserted", "");
}
} catch (SQLiteException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
if (exist != null) {
exist.close();
}
return exist != null ? true : false;
}
private void OpenDataBase() throws SQLDataException {
if (ChekDB()) {
String DBPath = Path + DataBase_Name;
DB = SQLiteDatabase.openDatabase(DBPath, null, SQLiteDatabase.OPEN_READWRITE);
} else {
this.getWritableDatabase();
try {
CopyDataBase();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public Cursor Select(String Query) {
try {
OpenDataBase();
cursor = DB.rawQuery(Query, null);
} catch (SQLDataException e) {
if (e!=null&&e.getMessage()!=null)
Log.e("Handler - Select :",e.getMessage());
}
return cursor;
}
/**
* @param tableName
* @param contentValues
* @param whereClause
* @param args
* @return Rows Effected
*/
protected int update(String tableName, ContentValues contentValues, String whereClause, String[] args) {
try {
OpenDataBase();
} catch (SQLDataException e) {
System.out.println(e.getMessage());
}
int result = DB.update(tableName, contentValues, whereClause, null);
DB.close();
return result;
}
/**
* @param tableName
* @param whereClause
* @param args
* @return Rows Effected
*/
protected int delete(String tableName, String whereClause, String[] args) {
try {
OpenDataBase();
} catch (SQLDataException e) {
System.out.println(e.getMessage());
}
int result = DB.delete(tableName, whereClause, args);
DB.close();
return result;
}
protected long insert(String Table_Name, ContentValues V) {
long rowid = -1;
try {
OpenDataBase();
rowid = DB.insert(Table_Name, null, V);
System.out.println(rowid);
DB.close();
} catch (Exception e) {
if (e!=null&&e.getMessage()!=null)
Log.e("Inserting to Table "+Table_Name,e.getMessage());
DB.close();
}
return rowid;
}
protected void ExecuteQuery(String Q) {
try {
OpenDataBase();
DB.execSQL(Q);
DB.close();
} catch (Exception e) {
System.out.println("ExecuteQuery " + e.getMessage());
}
}
public void Close() {
if (DB != null)
DB.close();
if (cursor != null && !cursor.isClosed())
cursor.close();
}
public void WhenUpGrade() {
try {
String assetPath = context.getAssets().toString() + "/" + DataBase_Name;
SQLiteDatabase assetDB = SQLiteDatabase.openDatabase(assetPath, null, SQLiteDatabase.OPEN_READWRITE);
SQLiteDatabase oldDB = SQLiteDatabase.openDatabase(Path + DataBase_Name, null, SQLiteDatabase.OPEN_READWRITE);
Log.d("", assetDB != null ? "" : "");
Log.d("", oldDB != null ? "" : "");
} catch (Exception e) {
if (e != null && e.getMessage() != null)
Log.e("upgradingDB", e.getMessage());
}
}
}