1、
public static final String DATABASE_FILENAME = "stock.db"; // 這個是DB文件名字
public static final String PACKAGE_NAME = "com.huaxa.it.tuling"; // 這個是自己項目包路徑
public static final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/"
+PACKAGE_NAME; // 獲取存儲位置地址
2、將res/raw的db文件讀取出來,然後在手機某個目錄下並生成db文件
private SQLiteOpenHelper sqlHelper;
private static SQLiteDatabase database;
private String name = ""; // 股票搜索出來的代碼
static String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
public static SQLiteDatabase openDatabase(Context context)
{
try
{
// String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
if (!dir.exists())
{
dir.mkdir();//新建文件
}
if (!(new File(databaseFilename)).exists())
{
InputStream is = context.getResources().openRawResource(
R.raw.stock);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
database = SQLiteDatabase.openOrCreateDatabase(databaseFilename,
null);
return database;
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
3、操作數據庫// 獲取股票代碼
public void getGuPiaoDaiMA(String str)
{
SQLiteDatabase db = openDatabase(this);
Cursor c = db.rawQuery("select * from db where name=?" ,new String[]{"南方航空"});//此處的db是表名,name是列名
c.moveToFirst();
while (!c.isAfterLast())
{
name = c.getString(c.getColumnIndex("代碼"));
c.moveToNext();
}
}