-2

**我正在使用一個退出的數據庫。首先,我在sqlite管理器中創建一個數據庫,然後將其放入斷言。 ** You can see my database screen shoot and here also exits my table which i am calling DBhelper classandroid.database.sqlite.SQLiteException:沒有這樣的表:liongenman16b1(Sqlite代碼1):,編譯時:

**錯誤是**

android.database.sqlite.SQLiteException: no such table: liongenman16b1(Sqlite code 1): , while compiling: SELECT DISTINCT street FROM liongenman16b1 ORDER BY street,(OS error - 2:No such file or directory) 
                          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:1334) 
                          at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1268) 
                          at com.example.rehankhan.parkken_app.Database.DBhelper.Get_Street(DBhelper.java:74) 
                          at com.example.rehankhan.parkken_app.Fragments.Search_Frag.onCreateView(Search_Frag.java:80) 
                          at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) 
                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) 
                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) 
                          at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
                          at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643) 
                          at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679) 
                          at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) 
                          at android.support.v4.view.ViewPager.populate(ViewPager.java:1272) 
                          at android.support.v4.view.ViewPager.populate(ViewPager.java:1120) 
                          at android.support.v4.view.ViewPager$3.run(ViewPager.java:277) 
                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:799) 
                          at android.view.Choreographer.doCallbacks(Choreographer.java:612) 
                          at android.view.Choreographer.doFrame(Choreographer.java:580) 
                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:785) 
                          at android.os.Handler.handleCallback(Handler.java:739) 
                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                          at android.os.Looper.loop(Looper.java:135) 
                          at android.app.ActivityThread.main(ActivityThread.java:5593) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at java.lang.reflect.Method.invoke(Method.java:372) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

我DBHelper

package com.example.rehankhan.parkken_app.Database; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

import com.example.rehankhan.parkken_app.Model.mapelmt; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Rehan Khan on 3/13/2017. 
*/ 
public class DBhelper extends SQLiteOpenHelper { 

private Context acontext; 
    public static final String  DBNAME="mapdata.sqlite"; 
public static final String TABLE="liongenman16b1"; 
    public static final String DBLOCATION="/data/data/com.example.rehankhan.parkken_app/databases/"; 
    private SQLiteDatabase mdatabase; 
    public DBhelper(Context context) 
    { 
     super(context, "mapdata.sqlite",null, 1); 
     this.acontext=context; 
    } 

    public void openDatabase(){ 
     String dbpath=acontext.getDatabasePath(DBNAME).getPath(); 
     if(mdatabase !=null && mdatabase.isOpen()){ 
      return; 
     } 
     File file = new File(dbpath); 
     if (file.exists() && !file.isDirectory()) 
      mdatabase = SQLiteDatabase.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READWRITE); 

     // mdatabase = SQLiteDatabase.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READWRITE); 

    } 
    public void closeDatabase(){ 
     if(mdatabase!=null){ 
      mdatabase.close(); 
     } 
    } 


    @Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

    } 


public List<mapelmt> Get_Street(){ 


     List<mapelmt> MapelmentsArry=null; 
     long result; 
     String course,time,room; 
     openDatabase(); 
     SQLiteDatabase db = getReadableDatabase(); 

     // Toast.makeText(get, "Your BMI is " + result, Toast.LENGTH_SHORT).show(); 
     String query = "SELECT DISTINCT street FROM " + TABLE + " ORDER BY street"; 
    //String query = "SELECT DISTINCT street FROM liongenman16b1 ORDER BY street"; 

光標光標= db.rawQuery(查詢,NULL); //這一行有錯誤DBhelper.java:67

 if(cursor.moveToNext()) { 


      MapelmentsArry = new ArrayList<>(); 
      do { 


       mapelmt data = new mapelmt(); 
///okk here 2 
       data.setStreet(cursor.getString(cursor.getColumnIndex("street"))); 

       MapelmentsArry.add(data); 
      }while (cursor.moveToNext()); 
     } 

     else{ 
      return null; 
     } 
     // Log.e("arry", String.valueOf(time_table.length)); 
     cursor.close(); 
     closeDatabase(); 
     return MapelmentsArry; 
    } 


} 

** MY fregment課堂,我打電話DBhelper類。我知道這是不是非常有必要知道你**

package com.example.rehankhan.parkken_app.Fragments; 

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListView; 

import com.example.rehankhan.parkken_app.Adapter.MapElementAdapter; 
import com.example.rehankhan.parkken_app.Database.DBhelper; 
import com.example.rehankhan.parkken_app.Model.mapelmt; 
import com.example.rehankhan.parkken_app.R; 

import java.util.List; 

/** 
* A simple {@link Fragment} subclass. 
* Activities that contain this fragment must implement the 
* {@link Search_Frag.OnFragmentInteractionListener} interface 
* to handle interaction events. 
* Use the {@link Search_Frag#newInstance} factory method to 
* create an instance of this fragment. 
*/ 
public class Search_Frag extends Fragment { 
    // TODO: Rename parameter arguments, choose names that match 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 
    private ListView listView; 
    MapElementAdapter adapter; 
    private List<mapelmt> streetList; 
    DBhelper data; 
    public Search_Frag() { 
     // Required empty public constructor 
    } 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @param param1 Parameter 1. 
    * @param param2 Parameter 2. 
    * @return A new instance of fragment Search_Frag. 
    */ 
    // TODO: Rename and change types and number of parameters 
    public static Search_Frag newInstance(String param1, String param2) { 
     Search_Frag fragment = new Search_Frag(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v= inflater.inflate(R.layout.fragment_search_, container, false); 
     listView=(ListView) v.findViewById(R.id.street_list); 
     data=new DBhelper(this.getContext()); 
     streetList=data.Get_Street(); //this error line (Search_Frag.java:80) 
     if(streetList!=null) 
     { 
      adapter=new MapElementAdapter(this.getContext(),streetList); 
      ///set Adapter 
      listView.setAdapter(adapter); 
      // Toast.makeText(getBaseContext(),"you couse"+inset_Course,Toast.LENGTH_LONG).show(); 
     } 


      return v; 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 



    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    /** 
    * This interface must be implemented by activities that contain this 
    * fragment to allow an interaction in this fragment to be communicated 
    * to the activity and potentially other fragments contained in that 
    * activity. 
    * <p> 
    * See the Android Training lesson <a href= 
    * "http://developer.android.com/training/basics/fragments/communicating.html" 
    * >Communicating with Other Fragments</a> for more information. 
    */ 
    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

回答

1

我認爲你錯過了創建表的代碼。

只需添加該代碼

 @Override 
     public void onCreate(SQLiteDatabase db) { 
      Log.e(TAG, "onCreate Database");  
      db.execSQL("# Add here your create table code "); 
     } 

EDITED ANSWER

然後,你需要遵循這個代碼。

public static class DataBaseHelper extends SQLiteOpenHelper { 

      private static Context mContext; 
      private static String DB_PATH = ""; 
      private static final String ASSETS_DATABASE_NAME = "mapdata.sqlite"; 

      private DataBaseHelper(Context context) { 
       super(context, DBNAME, null, 1); 
       mContext = context; 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
        DB_PATH = context.getApplicationInfo().dataDir + "/databases/"; 
       } else { 
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
       } 

        new Handler().post(new Runnable() { 

         @Override 
         public void run() { 
          // If u want to Copy Database from Assets. 
          try { 
           CopyAndCreateDataBase(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }); 
      } 


      // If database not exists copy it from the assets 
      public void CopyAndCreateDataBase() throws IOException { 

       boolean mDataBaseExist = checkDataBase(); 
       if (!mDataBaseExist) { 
        this.getReadableDatabase(); 
        this.getWritableDatabase(); 
        this.close(); 
        try { 
         // Copy the database from assests 
         copyDataBase(); 

         String mPath = DB_PATH + DBNAME; 

         Log.v(TAG, "Database created : " + mPath); 

        } catch (IOException mIOException) { 
         throw new Error("ErrorCopyingDataBase"); 
        } 
       } 
      } 


      // Check that the database exists here: /data/data/yourpackage/databases/DatabaseName 
      private boolean checkDataBase() { 
       File dbFile = new File(DB_PATH + DBNAME); 
       // Log.v("dbFile", dbFile + " "+ dbFile.exists()); 
       return dbFile.exists(); 
      } 

      // Copy the database from assets 
      private void copyDataBase() throws IOException { 
       InputStream mInput = mContext.getAssets().open(ASSETS_DATABASE_NAME); 
       String outFileName = DB_PATH + DBNAME; 
       OutputStream mOutput = new FileOutputStream(outFileName); 
       byte[] mBuffer = new byte[1024]; 
       int mLength; 
       while ((mLength = mInput.read(mBuffer)) > 0) { 
        mOutput.write(mBuffer, 0, mLength); 
       } 
       mOutput.flush(); 
       mOutput.close(); 
       mInput.close(); 
      } 
} 
+0

感謝幫助我@Dixit .Patel。我是用我投入現有的數據庫聲稱,它已經有這個表。那麼爲什麼我需要創建一個表 –

+0

確定剛剛看到我的編輯答案,你會得到你的回答。 –

+0

朋友我得到你,但我的問題仍然存在.please引導我.i複製此塊並過去它我的dbhelper類 –

相關問題