只是我test页面的代码
srbt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
query();
// srDb.close();
}
});
}
private void query()
{
try{
db.createDataBase();
srDb=db.getWritableDatabase();
// Cursor cs=srDb.query(false, null, null, null, null, null, null, null, null);
String str=srText.getText().toString();
String sql = "Select * from chengyucidian where chengyu='"+str+"'";
Cursor cs=srDb.rawQuery(sql, null);
cs.moveToFirst();
srText.setText(cs.getString(cs.getColumnIndex("pinyin")));
System.out.println(cs.getString(cs.getColumnIndex("pinyin")));
//srDb=new copyDB(this);
// srDb=SQLiteDatabase.openOrCreateDatabase("data/data/practice.content/data.db", null);
/*
//srDb=SQLiteDatabase.openOrCreateDatabase("/assets/data.db", null);
String str=srText.getText().toString();
String sql = "Select * from chengyucidian where chengyu='"+str+"'";
Cursor cr=srDb.rawQuery(sql, null);
cr.moveToFirst();
//for(int i=0;i<cr.getCount()-1)
srText.setText(cr.getString(cr.getColumnIndex("pinyin")));*/
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
System.out.println(e.toString());
}这是复制数据库到SDK上的代码 copyDB.class的代码
package practice.content;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.widget.Toast;public class copyDB extends SQLiteOpenHelper{ private static final int DB_VERSION=1;
private static final String DB_PATH="data/data/practice.content/";
// private static String DB_PATH=Environment.getExternalStorageDirectory()+"/data.db";
private static final String DB_NAME="data.db";
private static final String ASSETS_NAME="data.db";
private SQLiteDatabase mydb=null;
private Context mycontext;private final int ASSETS_START=101;
private final int ASSETS_END=110; public copyDB(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
this.mycontext=context;
}
public copyDB(Context context, String name,int version)
{
this(context,name,null,version);
}
public copyDB(Context context, String name)
{
this(context,name,DB_VERSION);
}
public copyDB(Context context)
{
this(context,DB_PATH+DB_NAME);
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//数据库已存在,do nothing.
}else{
//创建数据库
try {
File dir = new File(DB_PATH);
if(!dir.exists()){
dir.mkdirs();
}
File dbf = new File(DB_PATH + DB_NAME);
if(dbf.exists()){
dbf.delete();
}
SQLiteDatabase.openOrCreateDatabase(dbf, null);
// 复制asseets中的db文件到DB_PATH下
copyDataBase();
} catch (IOException e) {
// throw new Error("数据库创建失败");
// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
System.out.println(e+"");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
String myPath = DB_PATH + DB_NAME;
try{
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
System.out.println(e+"");
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = mycontext.getAssets().open(ASSETS_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
//复制assets下的大数据库文件时用这个
private void copyBigDataBase() throws IOException{
InputStream myInput;
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
for (int i = ASSETS_START; i < ASSETS_END+1; i++) {
myInput = mycontext.getAssets().open(ASSETS_NAME + "." + i);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myInput.close();
}
myOutput.close();
}
@Override
public synchronized void close() {
if(mydb != null){
mydb.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
} @Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}}
srbt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
query();
// srDb.close();
}
});
}
private void query()
{
try{
db.createDataBase();
srDb=db.getWritableDatabase();
// Cursor cs=srDb.query(false, null, null, null, null, null, null, null, null);
String str=srText.getText().toString();
String sql = "Select * from chengyucidian where chengyu='"+str+"'";
Cursor cs=srDb.rawQuery(sql, null);
cs.moveToFirst();
srText.setText(cs.getString(cs.getColumnIndex("pinyin")));
System.out.println(cs.getString(cs.getColumnIndex("pinyin")));
//srDb=new copyDB(this);
// srDb=SQLiteDatabase.openOrCreateDatabase("data/data/practice.content/data.db", null);
/*
//srDb=SQLiteDatabase.openOrCreateDatabase("/assets/data.db", null);
String str=srText.getText().toString();
String sql = "Select * from chengyucidian where chengyu='"+str+"'";
Cursor cr=srDb.rawQuery(sql, null);
cr.moveToFirst();
//for(int i=0;i<cr.getCount()-1)
srText.setText(cr.getString(cr.getColumnIndex("pinyin")));*/
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
System.out.println(e.toString());
}这是复制数据库到SDK上的代码 copyDB.class的代码
package practice.content;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.widget.Toast;public class copyDB extends SQLiteOpenHelper{ private static final int DB_VERSION=1;
private static final String DB_PATH="data/data/practice.content/";
// private static String DB_PATH=Environment.getExternalStorageDirectory()+"/data.db";
private static final String DB_NAME="data.db";
private static final String ASSETS_NAME="data.db";
private SQLiteDatabase mydb=null;
private Context mycontext;private final int ASSETS_START=101;
private final int ASSETS_END=110; public copyDB(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
this.mycontext=context;
}
public copyDB(Context context, String name,int version)
{
this(context,name,null,version);
}
public copyDB(Context context, String name)
{
this(context,name,DB_VERSION);
}
public copyDB(Context context)
{
this(context,DB_PATH+DB_NAME);
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//数据库已存在,do nothing.
}else{
//创建数据库
try {
File dir = new File(DB_PATH);
if(!dir.exists()){
dir.mkdirs();
}
File dbf = new File(DB_PATH + DB_NAME);
if(dbf.exists()){
dbf.delete();
}
SQLiteDatabase.openOrCreateDatabase(dbf, null);
// 复制asseets中的db文件到DB_PATH下
copyDataBase();
} catch (IOException e) {
// throw new Error("数据库创建失败");
// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
System.out.println(e+"");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
String myPath = DB_PATH + DB_NAME;
try{
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
System.out.println(e+"");
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = mycontext.getAssets().open(ASSETS_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
//复制assets下的大数据库文件时用这个
private void copyBigDataBase() throws IOException{
InputStream myInput;
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
for (int i = ASSETS_START; i < ASSETS_END+1; i++) {
myInput = mycontext.getAssets().open(ASSETS_NAME + "." + i);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myInput.close();
}
myOutput.close();
}
@Override
public synchronized void close() {
if(mydb != null){
mydb.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
} @Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}}
解决方案 »
- android怎么把一个view放入xml中
- android状态栏中信号强度图标的大小怎么修改?
- 平台上DDR的运行速率统计
- 【Android GPS & 基站定位需考虑的因素】
- 一个有关Android的页面的跳转的问题
- android 使用openfire服务器 用smack实现
- 【求救】类似脚本精灵的功能实现?必须使用NDK模拟么?使用NDK该如何调用各个接口函数?
- android 如何实现对事件的引用
- 如何替换触屏去点击Android的一个App应用上的一个button
- 实现音乐播放器listview点击跳转播放,请大神指教
- Android开发难题,和广播有关的。。。
- android图库读取SD卡中图片
不支持sd卡数据库DB_PATH 是存放到哪里了????????????保存到sdcard的话,非要这么做的话,就在程序启动copydb的处理中判断客户端版本,低于2.1的直接拷贝到data包下面,还有assets下的资源文件不能大于1MB,你要分割拷贝