SQL加入两个表

时间:2013-09-14 21:18:08

标签: android sql

最近我想学习如何从2个不同的数据库加入ANDROID SQL中的2个表。但是我无法满足我的结果。

这是我的Stirng:

String cursor = "Select " + PricelistDatabase.KEY_PHONENAME + " , "
        + PricelistDatabase.KEY_PRICE + " , "
        + PricelistDatabase.KEY_PRICE2 + " , "
        + AllPhonesDatabase.KEY_PHONENAME + " , "
        + AllPhonesDatabase.KEY_URL + " , "
        + AllPhonesDatabase.KEY_URL2 + " , "
        + AllPhonesDatabase.KEY_URL3 + " FROM "
        + PricelistDatabase.SQLITE_PRICELIST + " INNER JOIN "
        + AllPhonesDatabase.SQLITE_AllPhones + " ON "  
        + PricelistDatabase.KEY_PHONENAME + "="
        + AllPhonesDatabase.KEY_PHONENAME;

外观如何:

Select phone_name , phone_price , phone_price2 , phone_name , phone_picurl1 , phone_picurl2 , phone_picurl3 FROM PriceList INNER JOIN AllPhonesTable ON phone_name=phone_name

ERROR

09-15 00:17:07.641: E/SQLiteLog(1922): (1) no such table: AllPhonesTable
09-15 00:17:07.646: E/AndroidRuntime(1922): FATAL EXCEPTION: main
09-15 00:17:07.646: E/AndroidRuntime(1922): android.database.sqlite.SQLiteException: no such table: AllPhonesTable (code 1): , while compiling: Select phone_name , phone_price , phone_price2 , phone_name , phone_picurl1 , phone_picurl2 , phone_picurl3 FROM PriceList INNER JOIN AllPhonesTable ON phone_name=phone_name
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1090)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:663)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1420)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1359)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at com.androidarabia.utils.PricelistDatabase.listSamsung(PricelistDatabase.java:133)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at com.androidarabia.pricelist.Samsung.onCreateView(Samsung.java:61)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.View.measure(View.java:16059)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.View.measure(View.java:16059)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.View.measure(View.java:16059)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.View.measure(View.java:16059)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:639)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.View.measure(View.java:16059)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.View.measure(View.java:16059)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.View.measure(View.java:16059)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2414)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.View.measure(View.java:16059)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2133)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1286)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1497)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1183)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4863)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.Choreographer.doFrame(Choreographer.java:532)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-15 00:17:07.646: E/AndroidRuntime(1922):     at android.os.Handler.handleCallback(Handler.java:725)

UPDATE:

我决定将两个表放在同一个数据库中。但是现在我在这个声明中得到了一个错误:

String cursor = "SELECT phone_name ,phone_price ,phone_price2 ,phone_picurl1,phone_picurl2  " +
            ",phone_picurl3 FROM PriceList pl INNER JOIN AllPhonesTable ap " +
            "ON ap.phone_name = pl.phone_name";

logcat的:

09-15 19:07:22.377: E/AndroidRuntime(8023): FATAL EXCEPTION: main
09-15 19:07:22.377: E/AndroidRuntime(8023): android.database.sqlite.SQLiteException: ambiguous column name: phone_name (code 1): , while compiling: SELECT phone_name ,phone_price ,phone_price2 ,phone_picurl1,phone_picurl2  ,phone_picurl3 FROM PriceList pl INNER JOIN AllPhonesTable ap ON ap.phone_name = pl.phone_name
09-15 19:07:22.377: E/AndroidRuntime(8023):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

2 个答案:

答案 0 :(得分:0)

首先它明确地说没有这样的表名为AllPhonesTable请验证它的存在

第二个怎么做

SELECT pl.phone_name
  ,pl.phone_price
  ,pl.phone_price2
  ,apl.phone_name
  ,apl.phone_picurl1
  ,apl.phone_picurl2
  ,apl.phone_picurl3
FROM database1.PriceList pl
INNER JOIN database2.AllPhonesTable Apl
  ON ap.phone_name = apl.phone_name

答案 1 :(得分:0)

在MySql中,您可以在表名前加上数据库名称。也许这也适用于Android。 (我不知道)

SELECT ...
  FROM A.table t1
  JOIN B.table2 t2 ON t2.column = t1.column