我在我的android项目中导入一个SQLite数据库(进入assets文件夹)。我想访问它并显示记录。但是当我尝试它。得到那个错误
DatabaseHelper Class
public class DatabaseHelper extends SQLiteOpenHelper {
static String DB_PATH;
//Veritabanı ismini veriyoruz
static String DB_NAME = "ogrenci";
SQLiteDatabase myDatabase;
final Context myContext;
public DatabaseHelper(Context context)
{
super(context, DB_NAME, null, 1);
DB_PATH = context.getFilesDir().getParent() + "/databases/";
this.myContext = context;
}
//Assest dizinine koyduğumuz sql dosyasını, Android projesi içine oluşturma ve kopyalamna işlemi yapıldı..
public void CreateDataBase()
{
boolean dbExists = checkDataBase();
if (!dbExists)
{
this.getReadableDatabase();
try
{
copyDataBase();
}
catch (Exception ex)
{
Log.w("hata","Veritabanı kopyalanamıyor");
throw new Error("Veritabanı kopyalanamıyor.");
}
}
}
//Sqlite veritabanı dosyasını açıp, veritabanımı kontrol ediyoruz
boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
catch (Exception ex)
{
Log.w("hata","Veritabanı açılamadı");
}
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
}
///Assest dizinine koyduğumuz sql dosyasındaki verileri kopyalıyoruz
void copyDataBase()
{
try
{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myInput.close();
myOutput.close();
}
catch (Exception ex)
{
Log.w("hata", "Kopya oluşturma hatası.");
}
}
//Veritabannı açma işlemi yapıldı
void openDataBase()
{
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close()
{
if (myDatabase != null && myDatabase.isOpen())
myDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Corba1班
public class F_corba1 extends Fragment {
View parent;
Context con;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// text_baslik=(TextView)parent.findViewById(R.id.text_baslik);
parent=inflater.inflate(R.layout.corba1,container,false);
con=inflater.getContext();
//DatabaseHelper sınıfımızdaki CreateDataBase methodunu çağırıp , assest dizinine koymuş
//olduğumuz veritabanını,Android projesinin içinde oluşturup,verileri kopyalamasını sağladık...
DatabaseHelper dbHelper=new DatabaseHelper(con);
try
{
dbHelper.CreateDataBase();
}
catch (Exception ex)
{
Log.w("hata","Veritabanı oluşturulamadı ve kopyalanamadı!");
}
//Proje içine kopyalanmış olan veritabanımızdan verileri listview e yazdırdık
SQLiteDatabase db=dbHelper.getReadableDatabase();
String[] getColumnName={"student_name,student_surname"};
Cursor imlec=db.query("student", getColumnName, null, null, null, null, null);
ListView listview=(ListView)parent.findViewById(R.id.listview);
ArrayList<String> student=new ArrayList<String>();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(con,android.R.layout.simple_spinner_dropdown_item,student);
while(imlec.moveToNext()){
String student_name=imlec.getString(imlec.getColumnIndex("student_name"));
String student_surname=imlec.getString(imlec.getColumnIndex("student_surname"));
String name_surname= student_name+ " "+student_surname;
student.add(name_surname);
}
listview.setAdapter(adapter);
imlec.close();
db.close();
return parent;
}
}
错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yeni, PID: 26479
android.database.sqlite.SQLiteException: no such table: student (code 1): , while compiling: SELECT student_name,student_surname FROM student
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
at com.yeni.corbalar.F_corba1.onCreateView(F_corba1.java:63)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
错误行(F_corba1.java:63)
Cursor imlec=db.query("student", getColumnName, null, null, null, null, null);
答案 0 :(得分:1)
我认为你的数据库路径是错误的。在您的情况下,您可以在资产文件夹中获取数据库的路径,如下所示:
DB_PATH = "file:///android_asset/School.db"
或者您可以将数据库作为InputStream获取:
InputStream dbInputStream = context.getAssets().open("School.db");
答案 1 :(得分:1)
试试这个:
DatabaseHelper Class
public class DatabaseHelper extends SQLiteOpenHelper {
static String DB_PATH;
//Veritabanı ismini veriyoruz
static String DB_NAME = "School.db";
答案 2 :(得分:0)
用下面的代码替换你的代码。
public class F_corba1 extends Fragment {
View parent;
Context con;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// text_baslik=(TextView)parent.findViewById(R.id.text_baslik);
parent=inflater.inflate(R.layout.corba1,container,false);
con=inflater.getContext();
//DatabaseHelper sınıfımızdaki CreateDataBase methodunu çağırıp , assest dizinine koymuş
//olduğumuz veritabanını,Android projesinin içinde oluşturup,verileri kopyalamasını sağladık...
DatabaseHelper dbHelper=new DatabaseHelper(con);
try
{
dbHelper.CreateDataBase();
dbHelper.copyDataBase();
}
catch (Exception ex)
{
Log.w("hata","Veritabanı oluşturulamadı ve kopyalanamadı!");
}
//Proje içine kopyalanmış olan veritabanımızdan verileri listview e yazdırdık
SQLiteDatabase db=dbHelper.getReadableDatabase();
String[] getColumnName={"student_name,student_surname"};
Cursor imlec=db.query("student", getColumnName, null, null, null, null, null);
ListView listview=(ListView)parent.findViewById(R.id.listview);
ArrayList<String> student=new ArrayList<String>();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(con,android.R.layout.simple_spinner_dropdown_item,student);
while(imlec.moveToNext()){
String student_name=imlec.getString(imlec.getColumnIndex("student_name"));
String student_surname=imlec.getString(imlec.getColumnIndex("student_surname"));
String name_surname= student_name+ " "+student_surname;
student.add(name_surname);
}
listview.setAdapter(adapter);
imlec.close();
db.close();
return parent;
}
}
答案 3 :(得分:0)
你不能在资产文件夹中使用db。 assets文件夹只是为了读取您无法保存或更新此文件夹中的任何文件。
答案 4 :(得分:0)
您没有copied
数据库School.db
到ogrenci
。只需使用copyDataBase()
DatabaseHelper
方法调用onCreate()
方法。
试试这个:
@Override
public void onCreate(SQLiteDatabase db) {
copyDataBase();
}
希望这会有所帮助〜