使用片段从数据库SQLite获取数据

时间:2015-01-20 06:09:49

标签: java android

这是我的片段活动我在访问数据库时遇到错误我的代码是: -

public class Test extends Fragment {


View view;
private Cursor c;
private final String DB = "Canary";
private SQLiteDatabase db2;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    view = inflater.inflate(R.layout.test, container, false);
    Button b = (Button) view.findViewById(R.id.test_button);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(),"Listning",Toast.LENGTH_LONG).show();
        }
    });


        db2 = SQLiteDatabase.openOrCreateDatabase(DB, null);
        Log.d("Lee", "working her1");
        c=db2.rawQuery("SELECT * FROM business_master", null);
        Log.d("Lee", "working here2");
        c.moveToFirst();
        String id=c.getString(c.getColumnIndex("id"));
        Log.d("id", "" + id);

        String email=c.getString(c.getColumnIndex("email"));
        Log.d("email", email);
        String password=c.getString(c.getColumnIndex("password"));
        db2.close();

    return view;
}           }

我得到的错误: -

01-20 11:35:57.168  17081-17081/com.purplefront.canary E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.purplefront.canary, PID: 17081
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
        at com.purplefront.canary.Test.onCreateView(Test.java:36)
        at android.app.Fragment.performCreateView(Fragment.java:1700)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
        at android.app.BackStackRecord.run(BackStackRecord.java:684)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
        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:5086)
        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)

如果我删除了数据库访问,它将按预期正常工作,但我只需要片段访问数据库。

任何帮助将不胜感激。

0 个答案:

没有答案