2014-02-06 73 views
0

我試圖製作一個Android應用程序,我可以選擇一個地方(一家企業)並使用Google地圖進行打開。我先試着用一個數組來檢查它是否工作,並且它正常工作。所以,在那之後,我想讓它變得更好,並創建一個只有一個表的數據庫,以在應用程序和數據庫之間進行交互。數據庫名稱爲dbEmpresas.db,表名爲empresas試圖從Android上的SQLite表中獲取數據...我做錯了什麼?

MainActivity.java

public class MainActivity extends Activity{ 
    private Button conf; 
    private Spinner cb; 
    private String busqueda; 
    private BDAdapter bd; 
    private int i = 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     cb = (Spinner) findViewById(R.id.cbBizkaia); 
     conf = (Button)findViewById(R.id.bConf); 

     llenarCB(); 

     conf = (Button)findViewById(R.id.bConf); 
     conf.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       buscarMapa(cb.getSelectedItem().toString()); 
      } 
     }); 
    } 

    private void llenarCB() { 
     try{ 
      bd = new BDAdapter(this); 
      bd.open(); 
      Cursor s = bd.getEmpresas(); 
      String Empre[] = new String[bd.contEmp()]; 
      //String s[] = {"Beertecni","Linersa", "Técnicas Hidráulicas"}; 
      while(s.moveToNext()) { 
       Empre[i] = s.getString(0); 
       i++; 
      } 
      ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, Empre); 
      cb.setAdapter(dataAdapter); 
      bd.close(); 
     } catch (SQLiteException e) { 
      System.out.println("Error "+e); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    private void buscarMapa(String empresa) {  
     String dire = bd.getEnlace(empresa).toString(); 

     Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:0,0?q="+busqueda)); 
     intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity")); 
     startActivity(intent); 
    } 
} 

BDAdapter.java

public class BDAdapter{ 

    static final String TAG = "BDAdapter"; 
    static final String DATABASE_NAME = "dbEmpresas.db"; 
    static final int DATABASE_VERSION = 2; 
    final Context context; 
    int equiSel; 

    DatabaseHelper DBHelper; 
    SQLiteDatabase db; 

    public BDAdapter(Context ctx){ 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper{ 
     DatabaseHelper(Context context){ 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 
     @Override 
     public void onCreate(SQLiteDatabase db) { 
      // TODO Auto-generated method stub 
     } 
     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 
     } 
    } 

    public BDAdapter open() throws SQLException{ 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close(){ 
     DBHelper.close(); 
    } 

    Cursor getEmpresas(){ 
     return db.rawQuery("SELECT nombre FROM empresas;", null); 
    } 

    Cursor getEnlace(String nombre) { 
     return db.rawQuery("SELECT enlace FROM empresas where nombre='"+nombre+"';", null); 
    } 

    public int contEmp(){ 
     db = DBHelper.getReadableDatabase(); 
     Cursor cursor = db.rawQuery("SELECT count(*) FROM empresas;", null); 
     int cont = cursor.getInt(0); 
     db.close(); 
     return cont; 
    } 
} 

我一直在尋找了幾個小時,我還沒有發現任何有用的東西,任何幫助,好嗎?

logcat的

02-06 12:21:41.815: E/AndroidRuntime(962): FATAL EXCEPTION: main 
02-06 12:21:41.815: E/AndroidRuntime(962): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maps/com.example.maps.MainActivity}: android.database.sqlite.SQLiteException: no such table: empresas: , while compiling: SELECT nombre FROM empresas; 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.os.Looper.loop(Looper.java:123) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-06 12:21:41.815: E/AndroidRuntime(962): at java.lang.reflect.Method.invokeNative(Native Method) 
02-06 12:21:41.815: E/AndroidRuntime(962): at java.lang.reflect.Method.invoke(Method.java:521) 
02-06 12:21:41.815: E/AndroidRuntime(962): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-06 12:21:41.815: E/AndroidRuntime(962): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-06 12:21:41.815: E/AndroidRuntime(962): at dalvik.system.NativeStart.main(Native Method) 
02-06 12:21:41.815: E/AndroidRuntime(962): Caused by: android.database.sqlite.SQLiteException: no such table: empresas: , while compiling: SELECT nombre FROM empresas; 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315) 
02-06 12:21:41.815: E/AndroidRuntime(962): at com.example.maps.BDAdapter.getEmpresas(BDAdapter.java:49) 
02-06 12:21:41.815: E/AndroidRuntime(962): at com.example.maps.MainActivity.llenarCB(MainActivity.java:44) 
02-06 12:21:41.815: E/AndroidRuntime(962): at com.example.maps.MainActivity.onCreate(MainActivity.java:30) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-06 12:21:41.815: E/AndroidRuntime(962): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
02-06 12:21:41.815: E/AndroidRuntime(962): ... 11 more 
+1

你的SQLiteOpenHelper類的'onCreate'和'onUpgrade'方法中沒有任何東西你確定你正在創建數據庫表嗎? – SMR

+1

如果您有例外,請發佈logcat。快速瀏覽,代碼存在多個問題。請說明您面臨的具體問題。 – laalto

+0

我不想創建一個新表,我只是試圖從我的數據庫上的一個表中接收數據。 我已將logcat添加到原始帖子中 – Yespy

回答

0

您尚未創建數據庫中的任何表!看看這個來看看create table語句和它們的執行位置。 Similar question

相關問題