此代码是dbhelper类,用于从我的系统C:和db name School中的SQLite db获取数据。方法getinformation是抛出错误表不存在。有人可以帮助相同的。此代码是dbhelper类,用于从我的系统C:和db name School中的SQLite db获取数据。方法getinformation是抛出错误表不存在。有人可以帮助相同的。 此代码是dbhelper类,用于从我的系统C:和db name School中的SQLite db获取数据。方法getinformation是抛出错误表不存在。有人可以帮助解决相同问题。
package com.example.rabin_pc.myproject;
import android.app.Notification;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static android.content.ContentValues.TAG;
import static android.database.sqlite.SQLiteDatabase.*;
import static android.database.sqlite.SQLiteDatabase.openOrCreateDatabase;
/**
* Created by Rabin_PC on 14-Jul-17.
*/
public class CollegeDBHelper extends SQLiteOpenHelper {
private static String DB_PATH;
private static String DB_NAME="School.sqllite";
private static final int DB_VERSION=1;
private SQLiteDatabase myDataBase;
private Context myContext=null;
public CollegeDBHelper(Context context){
super(context,DB_NAME,null,DB_VERSION);
// log.e("DATABASE OPERATIONS" , "DATABASE OPENED");
}
public CollegeDBHelper(Context ctx,String databaseName) {
super(ctx, databaseName, null, DB_VERSION);
DB_NAME = "School.db";
this.myContext = ctx;
//DATABASE_PATH = context.getDatabasePath(DATABASE_NAME).getPath() ;
DB_PATH = "C:\\Users\\Rabin_PC\\Documents\\";
}
public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();
if(dbExist){
//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();
} catch (IOException e)
{
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase()
{ SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
Log.e(TAG,myPath);
checkDB = openDatabase(myPath, null, OPEN_READONLY);
}catch (SQLiteException e)
{
//database does't exist yet.
}
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(DB_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();
}
public void openDataBase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = openDatabase(myPath, null, OPEN_READWRITE);
}
@Override
public synchronized void close()
{
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
public Cursor getInformation(SQLiteDatabase db)
{
Cursor cursor;
String[] projections = {CollegeContract.newCollegeContract.COLLEGE_NAME, CollegeContract.newCollegeContract.COLLEGE_ADDRESS};
// System.out.print("++++Before executing++++++++++++++ 4444444444444444");
cursor = db.query(CollegeContract.newCollegeContract.TABLE_NAME, projections, null, null, null, null, null);
// System.out.print("++++Before executing++++++++++++++ 555555555555555");
return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
答案 0 :(得分:0)
首先在onCreate()
中创建您的表格并且您没有调用createDataBase()
方法并在onUpgrade()
drop table中再次调用onCreate方法并且DB_NAME
是不同的您使用了两个DB_NAME School.sqllite
和School.db
可能是因为将其显示错误并且没有初始化DATABASE_NAME
我将提供一个基本的SQLiteOpenHelper类
public class mySQLiteHelper extends SQLiteOpenHelper {
private static String DB_NAME="School.sqllite";
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "( " + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_COMMENT
+ " text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
onCreate(db);
}
}