我在服务器上有一个数据库作为应用内追求。 下载:
private static boolean downloadDatabase(Context context) {
try {
// Log.d(TAG, "downloading database");
URL url = new URL("http://url/");
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = null;
// Select storage location
fos = context.openFileOutput(Global.getName(), Context.MODE_PRIVATE);
fos.write(baf.toByteArray());
fos.close();
// Log.d(TAG, "downloaded");
} catch (IOException e) {
Log.e(TAG, "downloadDatabase Error: " , e);
return false;
} catch (NullPointerException e) {
Log.e(TAG, "downloadDatabase Error: " , e);
return false;
} catch (Exception e){
Log.e(TAG, "downloadDatabase Error: " , e);
return false;
}
return true;
这是DBHelper,数据库正在复制到data / data / ... dir。
public class DBHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/mypackage/databases";
public static String DB_NAME = Global.getName();
private static final int DB_VERSION = 1;
private static final String SP_KEY_DB_VER = "db_ver";
private SQLiteDatabase db;
private Context context;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
initialize();
}
private void initialize() {
if (databaseExists()) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
int dbVersion = prefs.getInt(SP_KEY_DB_VER, 1);
if (DB_VERSION != dbVersion) {
File dbFile = context.getDatabasePath(DB_NAME);
if (!dbFile.delete()) {
Log.w("MY APP", "Unable to update database");
}
}
}
if (!databaseExists()) {
createDatabase();
}
}
private boolean databaseExists() {
File dbFile = context.getDatabasePath(DB_NAME);
Log.e("TAG","database exist?"+dbFile.exists());
return dbFile.exists();
}
/**
* Creates database by copying it from assets directory.
*/
private void createDatabase() {
String parentPath = context.getDatabasePath(DB_NAME).getParent();
String path = context.getDatabasePath(DB_NAME).getPath();
File file = new File(parentPath);
if (!file.exists()) {
if (!file.mkdir()) {
Log.w("MY APP", "Unable to create database directory");
return;
}
}
InputStream is = null;
OutputStream os = null;
try {
is = context.openFileInput(Global.getDictionary());
os = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
db= SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(db != null)
db.close();
super.close();
}
public String getStr() {
databaseExists();
SQLiteDatabase db = this.getReadableDatabase();
String s=null;
Cursor c = db.rawQuery("SELECT indexStr from tablename where id=2 ",null);
while (c.moveToNext()) {
s = c.getString(0);
}
c.close();
db.close();
return s;
}
@Override
public void onCreate(SQLiteDatabase arg0) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
我看到,我的应用程序在下载数据库后占用了更多的内存空间,但是当我运行任何SELECT时返回“(1)没有这样的表”,但数据库存在。可能是数据库空了,但为什么呢? 附:写入WRITE_EXTERNAL_STORAGE / READ_EXTERNAL_STORAGE的所有权限。
答案 0 :(得分:0)
我认为你的表名不正确
Cursor c = db.rawQuery("SELECT indexStr from tablename where id=2 ",null);
表的名称确实是" tablename
" ???
检查
的值public static String DB_NAME = Global.getName();
可能你正在阅读不同的数据库!。
答案 1 :(得分:0)
尝试将此代码添加到onCreate()方法并查看错误是否消失。
SQLiteDatabase db2 = openOrCreateDatabase("dbname", Context.MODE_PRIVATE, null);
db2.execSQL("CREATE TABLE IF NOT EXISTS table_name(row1 INT PRIMARY KEY, row2 VARCHAR, row3 VARCHAR, row4 VARCHAR, );");
如果是,那意味着您的表格不是按照您之前尝试的方式创建的。
检查数据库是否已填充的最佳方法是单击DDMS并检查是否在项目下创建了数据库文件。如果已创建,请从设备中提取文件,然后通过SQLite数据库浏览器查看该文件,例如SQLiteBrowser