如何在android集中的sqlite数据库中连接" / SDCard / myapp"
由于
答案 0 :(得分:0)
尝试this此类为开发人员提供了一种使用现有SQLite数据库发布其Android应用的简单方法
答案 1 :(得分:0)
请求自己的许可。
获取db的方法:
package com.airi.buyue.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import com.airi.buyue.BuyueApp;
import com.airi.buyue.R;
import com.airi.buyue.util.FileUtils;
import com.airi.buyue.util.SystemUtils;
import com.airi.buyue.util.Utilities;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataManager {
public static final String DB_NAME = "zonen.db";
public static final String TABLE_NAME = "m_nzone_copy";
private SQLiteDatabase database;
private Context context;
public DataManager(Context context) {
this.context = context;
getPath();
}
public SQLiteDatabase getDatabase() {
return database;
}
public void setDatabase(SQLiteDatabase database) {
this.database = database;
}
public void openDatabase() {
this.database = this.openDatabase(getPath() + "/" + DB_NAME);
}
public String getPath(){
String DATABASE_PATH;
DATABASE_PATH = Environment.getExternalStorageDirectory().getPath()+"/"+"myapp";
return DATABASE_PATH;
}
private SQLiteDatabase openDatabase(String dbfile) {
SystemUtils.debugLog("test-zonedb","attempt");
try {
if (!(new File(dbfile).exists())) {
//do sth
}
FileUtils.updateFile(dbfile);
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,null);
return db;
} catch (FileNotFoundException e) {
SystemUtils.debugLog("test-zonedb","file no found");
SystemUtils.attemptPrintError(e);
} catch (IOException e) {
SystemUtils.debugLog("test-zonedb","io expection");
SystemUtils.attemptPrintError(e);
}
return null;
}
public void closeDatabase() {
if(this.database!=null){
this.database.close();
}
}
}
答案 2 :(得分:0)
在班级中定义:
public static SQLiteDatabase database;
public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath(); //adress SD CARD ro migire
public static final String DIR_DATABASE = DIR_SDCARD + "/myapp/";
然后,写入你的onCreate:
context = this.getApplicationContext();
new File(DIR_DATABASE).mkdirs();
用于创建数据库:
database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/myapp", null);
数据库中的crating表:
database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/myapp.sqlite", null); //database ro misaze
database.execSQL("CREATE TABLE IF NOT EXISTS person (" +
"person_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , " +
"person_name TEXT, " +
"person_family TEXT, " +
"person_age INTEGER" +
")")
用于插入数据库:
for (int i = 0; i < 20; i++) {
database.execSQL("INSERT INTO person (person_name,person_family,person_age) VALUES ('Name#" + i + "','Family#" + i + "'," + i + ")");
}
从数据库中读取:
Cursor cursor = database.rawQuery("SELECT * FROM person WHERE person_age>10", null);
while (cursor.moveToNext()) { //age betoone bere be badi, ta zamani ke cursor ee vojud dashte bashe in karo mikone
String name = cursor.getString(cursor.getColumnIndex("person_name"));
String family = cursor.getString(cursor.getColumnIndex("person_family"));
int age = cursor.getInt(cursor.getColumnIndex("person_age"));
Log.i("LOG", "Record: " + name + " " + family + ", " + age);
}
cursor.close();
用于删除和更新:
database.execSQL("DELETE FROM person WHERE person_age>10");
database.execSQL("UPDATE person SET person_name='test' WHERE person_age>7");