我有db xxx.sqlite(8Mo)我想在我的android项目中使用所以我使用这个代码但是它不起作用 Datahelper.java
public class Datahelper extends SQLiteOpenHelper {
private String dbName;
private String db_path;
private Context context;
public Datahelper(Context context, String dbName) {
super(context, dbName, null, 1);
this.dbName = dbName;
this.context = context;
db_path = "/data/data" + context.getPackageName() + "/databases/";
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
public boolean checkExist() {
SQLiteDatabase checkDB = null;
try {
String myPath = db_path + dbName;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
// database does't exist yet.
} catch (Exception ep) {
ep.printStackTrace();
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public int importIfNotExist() throws IOException {
int a=0;
boolean dbExist = checkExist();
if (dbExist) {
a=0;
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDatabase();
a=2;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
return a;
}
private void copyDatabase() throws IOException {
InputStream is = context.getAssets().open(dbName);
OutputStream os = new FileOutputStream(db_path + dbName);
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
this.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
} }
在我的activity.java中我放了
Datahelper dbHelper = new Datahelper(
getBaseContext(), "xxx.sqlite");
try {
dbHelper.importIfNotExist();
} catch (IOException e) {
e.printStackTrace();
}
我的一些朋友告诉我,我的数据库是非常大的8Mo,我试着把它放在res / raw中它不起作用,我把它放在assets文件夹中,它不起作用。 请有人帮忙。
答案 0 :(得分:0)
修改强>
检查此tutorial
您可以使用此功能获取旧数据库中的所有数据,替换" COLUMN_NAME"与你自己。
public class CustomProgressView extends View {
private int progress;
private int maxProgress;
private float arcWidth;
private float arcPadding;
private Paint paintPositive;
private Paint paintNegative;
private Paint paintText;
private Path path;
private Path clipPath;
private ProgressListener listener;
public CustomProgressView(Context context) {
super(context);
init();
}
public CustomProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
arcWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getResources().getDisplayMetrics());
arcPadding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, getResources().getDisplayMetrics());
paintPositive = new Paint();
paintPositive.setColor(Color.RED);
paintPositive.setStyle(Paint.Style.FILL_AND_STROKE);
paintPositive.setAntiAlias(true);
paintNegative = new Paint();
paintNegative.setColor(Color.BLUE);
paintPositive.setStyle(Paint.Style.FILL_AND_STROKE);
paintNegative.setAntiAlias(true);
paintText = new Paint();
paintText.setColor(Color.BLACK);
paintText.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 24, getResources().getDisplayMetrics()));
progress = 0;
maxProgress = 10;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
float diameter = Math.min(getWidth(), getHeight());
RectF ovalOuter = new RectF(0, 0, diameter, diameter);
RectF ovalInner = new RectF(ovalOuter.left + arcWidth, ovalOuter.top + arcWidth, ovalOuter.right - arcWidth, ovalOuter.bottom - arcWidth);
path = new Path();
path.moveTo(ovalOuter.centerX(), ovalOuter.top);
path.addArc(ovalOuter, 270, 90);
path.lineTo(ovalInner.right, ovalInner.centerY());
path.addArc(ovalInner, 0, -90);
path.lineTo(ovalOuter.centerX(), ovalOuter.top);
clipPath = new Path();
clipPath.addRect(ovalOuter.left, ovalOuter.centerY() - arcPadding / 2, ovalOuter.right, ovalOuter.centerY() + arcPadding / 2, Path.Direction.CW);
clipPath.addRect(ovalOuter.centerX() - arcPadding / 2, ovalOuter.top, ovalOuter.centerX() + arcPadding / 2, ovalOuter.bottom, Path.Direction.CW);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float perc = (float) progress / (float) maxProgress;
int state = 0;
if (perc < 0.25) {
state = 1;
} else if (perc < 0.5) {
state = 2;
} else if (perc < 0.75) {
state = 3;
} else {
state = 4;
}
RectF bounds = new RectF();
path.computeBounds(bounds, true);
// Draw Circle
canvas.save();
// Clip padding
canvas.clipPath(clipPath, Region.Op.DIFFERENCE);
canvas.drawPath(path, state == 1 ? paintPositive : paintNegative);
canvas.rotate(90, bounds.left, bounds.bottom);
canvas.drawPath(path, state == 2 ? paintPositive : paintNegative);
canvas.rotate(90, bounds.left, bounds.bottom);
canvas.drawPath(path, state == 3 ? paintPositive : paintNegative);
canvas.rotate(90, bounds.left, bounds.bottom);
canvas.drawPath(path, state == 4 ? paintPositive : paintNegative);
canvas.restore();
// Draw Progress
String text = String.valueOf(progress);
Rect textBounds = new Rect();
paintText.getTextBounds(text, 0, text.length(), textBounds);
float x = bounds.left - textBounds.width() / 2;
float y = bounds.bottom + textBounds.height() / 2;
canvas.drawText(text, x, y, paintText);
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
int oldProgress = this.progress;
this.progress = progress;
if (listener != null) {
listener.onProgressChanged(oldProgress, progress);
}
invalidate();
}
public int getMaxProgress() {
return maxProgress;
}
public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress;
invalidate();
}
public ProgressListener getListener() {
return listener;
}
public void setListener(ProgressListener listener) {
this.listener = listener;
}
public interface ProgressListener {
void onProgressChanged(int oldProgress, int newProgress);
}
}
使用此选项将数据保存在txt中
public boolean consulta(String titulo, String issn){
String a="",b="",c="";
boolean respuesta = false;
cursor = db.rawQuery("select * from "+ TABLA_NAME, null);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
a = cursor.getString(cursor.getColumnIndex("COLUMN_NAME1"));
b = cursor.getString(cursor.getColumnIndex("COLUMN_NAME2"));
c = cursor.getString(cursor.getColumnIndex("COLUMN_NAME3"));
}while (cursor.moveToNext());
}
}
respuesta = a+"-"+b+"-"+c;
cursor.close(); // cierra cursor
return respuesta;
}
之后,在新的Android项目中使用此函数从txt文件中获取数据并存储在新数据库中。
private void saveData(String data){
try {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/dataFile.txt");
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(data);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(), "LISTO, se escribio en la tarjeta SD", Toast.LENGTH_SHORT).show();//mensaje de que se escribio
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();// mensaje de error
}
}
希望得到这个帮助。