我在这里是因为我在android上遇到了postgreSQL的问题.... 我之前编写了一个java类,一个连接到我的外部数据库(postgreSQL)的代码,可以发送和接收查询(它100%工作,我用MAC上的应用程序pgAdmin3测试了相同的查询,两者都有相同的结果) 。 当我看到这个工作真的很好时,我决定将该代码导出到我的android应用程序中...... 问题是: 那个查询
public static final String queryLojasCidadaoOrderByName = "SELECT * FROM public.lojas_cidadao ORDER BY nome_loja_cidadao";
不能在我的Android应用程序上运行...在java应用程序上它的工作正常! 但该查询
public static final String queryLojasCidadao = "SELECT * FROM public.lojas_cidadao ";
它在我的Android应用程序上工作!
ORDER BY有问题吗?
欢迎任何帮助! :)谢谢!
我正在运行的应用程序的日志:
05-22 11:13:06.625: D/AbsListView(21421): unregisterIRListener() is called
05-22 11:13:06.835: D/AbsListView(21421): onVisibilityChanged() is called, visibility : 4
05-22 11:13:06.845: D/AbsListView(21421): unregisterIRListener() is called
05-22 11:13:16.014: D/skia(21882): ---- fAsset->read(2074) returned 0
05-22 11:13:16.064: D/dalvikvm(21882): GC_FOR_ALLOC freed 78K, 6% free 17421K/18408K, paused 20ms, total 20ms
05-22 11:13:16.094: I/dalvikvm-heap(21882): Grow heap (frag case) to 25.807MB for 8294416-byte allocation
05-22 11:13:16.114: D/dalvikvm(21882): GC_FOR_ALLOC freed 1K, 4% free 25519K/26512K, paused 18ms, total 18ms
05-22 11:13:16.124: D/dalvikvm(21882): GC_CONCURRENT freed <1K, 4% free 25519K/26512K, paused 2ms+2ms, total 16ms
05-22 11:13:16.144: D/dalvikvm(21882): GC_FOR_ALLOC freed <1K, 4% free 25519K/26512K, paused 15ms, total 16ms
05-22 11:13:16.194: I/dalvikvm-heap(21882): Grow heap (frag case) to 43.603MB for 18662416-byte allocation
05-22 11:13:16.214: D/dalvikvm(21882): GC_FOR_ALLOC freed <1K, 3% free 43744K/44740K, paused 23ms, total 23ms
05-22 11:13:16.234: D/dalvikvm(21882): GC_CONCURRENT freed 0K, 3% free 43744K/44740K, paused 3ms+3ms, total 21ms
05-22 11:13:19.157: W/System.err(21882): java.lang.NumberFormatException: Invalid double: "a"
05-22 11:13:19.157: W/System.err(21882): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
05-22 11:13:19.167: W/System.err(21882): at java.lang.StringToReal.parseDouble(StringToReal.java:269)
05-22 11:13:19.167: W/System.err(21882): at java.lang.Double.parseDouble(Double.java:295)
05-22 11:13:19.167: W/System.err(21882): at com.example.basicmaponline.Intro$loadDatabase.doInBackground(Intro.java:102)
05-22 11:13:19.167: W/System.err(21882): at com.example.basicmaponline.Intro$loadDatabase.doInBackground(Intro.java:1)
05-22 11:13:19.167: W/System.err(21882): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-22 11:13:19.167: W/System.err(21882): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-22 11:13:19.167: W/System.err(21882): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-22 11:13:19.167: W/System.err(21882): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-22 11:13:19.167: W/System.err(21882): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-22 11:13:19.167: W/System.err(21882): at java.lang.Thread.run(Thread.java:856)
05-22 11:13:19.177: D/ASYNC(21882): entrei no if da listaLojas
05-22 11:13:19.227: D/libEGL(21882): loaded /system/lib/egl/libEGL_adreno200.so
05-22 11:13:19.237: D/libEGL(21882): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-22 11:13:19.237: D/libEGL(21882): loaded /system/lib/egl/libGLESv2_adreno200.so
05-22 11:13:19.247: I/Adreno200-EGL(21882): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: (CL3544079)
05-22 11:13:19.247: I/Adreno200-EGL(21882): Build Date: 03/28/13 Thu
05-22 11:13:19.247: I/Adreno200-EGL(21882): Local Branch: adreno_20130328
05-22 11:13:19.247: I/Adreno200-EGL(21882): Remote Branch:
05-22 11:13:19.247: I/Adreno200-EGL(21882): Local Patches:
05-22 11:13:19.247: I/Adreno200-EGL(21882): Reconstruct Branch:
05-22 11:13:19.297: D/OpenGLRenderer(21882): Enabling debug mode 0
05-22 11:13:22.320: D/AbsListView(21882): Get MotionRecognitionManager
05-22 11:13:22.330: D/AbsListView(21882): onVisibilityChanged() is called, visibility : 4
05-22 11:13:22.330: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:22.330: D/AbsListView(21882): onVisibilityChanged() is called, visibility : 0
05-22 11:13:22.330: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:22.340: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:22.340: I/endeffect(21882): AbsListView.onMeasure(), getWidth()=0, getHeight()=0, this=android.widget.GridView{43b8b2f8 VFED.VC. ......I. 0,0-0,0 #7f070003 app:id/grid}
05-22 11:13:22.380: D/dalvikvm(21882): GC_FOR_ALLOC freed 8263K, 20% free 36922K/46100K, paused 21ms, total 21ms
05-22 11:13:22.420: I/dalvikvm-heap(21882): Grow heap (frag case) to 45.942MB for 9437200-byte allocation
05-22 11:13:22.470: D/dalvikvm(21882): GC_FOR_ALLOC freed 1K, 17% free 46137K/55320K, paused 49ms, total 49ms
05-22 11:13:22.500: D/dalvikvm(21882): GC_CONCURRENT freed 27K, 17% free 46110K/55320K, paused 13ms+3ms, total 34ms
05-22 11:13:22.520: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:22.520: I/endeffect(21882): AbsListView.onLayout(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{43b8b2f8 VFED.VC. ......ID 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:22.520: I/endeffect(21882): AbsListView.onMeasure(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{43b8b2f8 VFED.VC. .F....ID 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:22.520: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:22.520: I/endeffect(21882): AbsListView.onLayout(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{43b8b2f8 VFED.VC. .F....ID 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:22.660: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:22.670: I/endeffect(21882): AbsListView.onMeasure(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{43b8b2f8 VFED.VC. .F....I. 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:22.670: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:22.670: I/endeffect(21882): AbsListView.onLayout(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{43b8b2f8 VFED.VC. .F....ID 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:22.740: E/SpannableStringBuilder(21882): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-22 11:13:22.740: E/SpannableStringBuilder(21882): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-22 11:13:27.435: D/LISTA(21882): ENTREI AQUI CARALHOOO NA LISTA
05-22 11:13:27.435: D/AbsListView(21882): Get MotionRecognitionManager
05-22 11:13:27.435: D/AbsListView(21882): onVisibilityChanged() is called, visibility : 4
05-22 11:13:27.435: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:27.445: D/AbsListView(21882): onVisibilityChanged() is called, visibility : 0
05-22 11:13:27.445: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:27.445: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:27.455: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:27.455: I/endeffect(21882): AbsListView.onMeasure(), getWidth()=0, getHeight()=0, this=android.widget.ListView{43b6a938 V.ED.VC. ......I. 0,0-0,0 #102000a android:id/list}
05-22 11:13:27.475: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:27.475: I/endeffect(21882): AbsListView.onLayout(), getWidth()=1080, getHeight()=1701, this=android.widget.ListView{43b6a938 V.ED.VC. ......ID 0,0-1080,1701 #102000a android:id/list}
05-22 11:13:27.585: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:27.585: I/endeffect(21882): AbsListView.onMeasure(), getWidth()=1080, getHeight()=1701, this=android.widget.ListView{43b6a938 V.ED.VC. ......I. 0,0-1080,1701 #102000a android:id/list}
05-22 11:13:27.585: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:27.595: I/endeffect(21882): AbsListView.onLayout(), getWidth()=1080, getHeight()=1701, this=android.widget.ListView{43b6a938 V.ED.VC. ......ID 0,0-1080,1701 #102000a android:id/list}
05-22 11:13:27.645: E/SpannableStringBuilder(21882): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-22 11:13:27.645: E/SpannableStringBuilder(21882): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-22 11:13:27.655: D/AbsListView(21882): onVisibilityChanged() is called, visibility : 4
05-22 11:13:27.655: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.428: D/AbsListView(21882): Get MotionRecognitionManager
05-22 11:13:30.428: D/AbsListView(21882): onVisibilityChanged() is called, visibility : 4
05-22 11:13:30.428: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.428: D/AbsListView(21882): onVisibilityChanged() is called, visibility : 0
05-22 11:13:30.428: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.438: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.438: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.438: I/endeffect(21882): AbsListView.onMeasure(), getWidth()=0, getHeight()=0, this=android.widget.GridView{42202b48 VFED.VC. ......I. 0,0-0,0 #7f070003 app:id/grid}
05-22 11:13:30.458: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.458: I/endeffect(21882): AbsListView.onLayout(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{42202b48 VFED.VC. ......ID 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:30.468: I/endeffect(21882): AbsListView.onMeasure(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{42202b48 VFED.VC. .F....ID 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:30.468: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.468: I/endeffect(21882): AbsListView.onLayout(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{42202b48 VFED.VC. .F....ID 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:30.508: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.518: I/endeffect(21882): AbsListView.onMeasure(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{42202b48 VFED.VC. .F....I. 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:30.518: D/AbsListView(21882): unregisterIRListener() is called
05-22 11:13:30.518: I/endeffect(21882): AbsListView.onLayout(), getWidth()=1080, getHeight()=1701, this=android.widget.GridView{42202b48 VFED.VC. .F....ID 0,0-1080,1701 #7f070003 app:id/grid}
05-22 11:13:30.568: E/SpannableStringBuilder(21882): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-22 11:13:30.568: E/SpannableStringBuilder(21882): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-22 11:13:30.568: D/AbsListView(21882): onVisibilityChanged() is called, visibility : 4
05-22 11:13:30.568: D/AbsListView(21882): unregisterIRListener() is called
我的Android应用程序的简介类:
package com.example.basicmaponline;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.mlab.android.basicoverlays.PostgreSQL;
import com.mlab.android.basicoverlays.SQLloja;
public class Intro extends Activity{
//SQLlistLoja listaLoja;
//HashMap<String, SQLloja> listaLoja;
ArrayList<SQLloja> listaLoja;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
//Intent openMainActivity = new Intent("com.example.basicmaponline.MAINACTIVITY"); //isto vem do ficheiro AndroidManifest.xml o "com.example....."
//startActivity(openMainActivity);
try {
listaLoja = new loadDatabase().execute().get();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(listaLoja !=null){
for(SQLloja loja : listaLoja){
Log.d("ASYNC25", loja.getNome() );
}
}
else{
Log.d("ASYNC25", "NAO GUARDOU!" );
}
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}finally{
Intent openMenu = new Intent("com.example.basicmaponline.MENU"); //isto vem do ficheiro AndroidManifest.xml o "com.example....."
openMenu.putExtra("listaLoja",listaLoja);
startActivity(openMenu);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//ourSong.release(); //release the music and we are done with you
finish();
}
//@SuppressWarnings("rawtypes")
public class loadDatabase extends AsyncTask<Void, Void, ArrayList<SQLloja>>{
@Override
//protected HashMap<String,SQLloja> doInBackground(Void... params) {
protected ArrayList<SQLloja> doInBackground(Void... params) {
// TODO Auto-generated method stub
//HashMap<String,SQLloja>listaLojas = new HashMap<String, SQLloja>();
ArrayList<SQLloja> listaLojas = new ArrayList<SQLloja>();
try{
PostgreSQL pSQL = new PostgreSQL();
//String sql = pSQL.getLojasCidadao();
String sql = PostgreSQL.queryLojasCidadaoOrderByName; // public static final String queryLojasCidadaoOrderByName = "SELECT * FROM public.lojas_cidadao ORDER BY nome_loja_cidadao";
Statement st = pSQL.getConnection().createStatement();
ResultSet rs = st.executeQuery(sql);
//Log.d("ASYNC2","Entrei na thread ASYNCTASK");
while(rs.next()){
int lcId = Integer.parseInt(rs.getString(1));
String lcNome=rs.getString(2);
String lcCP = rs.getString(3);
int lcDistrito = Integer.parseInt(rs.getString(4));
int lcConselho = Integer.parseInt(rs.getString(5));
double lcAltitude = Double.parseDouble(rs.getString(6));
double lcLongitude = Double.parseDouble(rs.getString(7));
String lcTelefone = rs.getString(8);
boolean lcEstado = Boolean.parseBoolean(rs.getString(9));
String lcRua = rs.getString(10);
SQLloja loja = new SQLloja(lcId,lcNome,lcCP,lcDistrito,lcConselho,lcAltitude,lcLongitude,lcTelefone,lcEstado,lcRua);
//listaLojas.put(loja.getNome(),loja.clone());
listaLojas.add(loja.clone());
String informacoesLoja = "Rua : "+lcRua+"\nC.P. : "+lcCP+"\nTel. : "+lcTelefone;
Log.d("ASYNC",lcNome+" Altitude = "+lcAltitude+" Longitude = "+lcLongitude+" Rua = "+lcRua);
}
//listaLoja = new SQLlistLoja(listaLojas);
rs.close();
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if(listaLojas!=null){
Log.d("ASYNC","entrei no if da listaLojas");
for(SQLloja loja : listaLojas){
Log.d("ASYNC", loja.getNome() );
}
}
else Log.d("ASYNC","listaLoja nula ! PQP !");
return listaLojas;
/*HashMap<String ,SQLloja> listaLojas = new HashMap<String, SQLloja>();
try {
PostgreSQL pSQL = new PostgreSQL();
listaLojas = pSQL.getLojasCidadao();
if(listaLojas !=null){
for(SQLloja loja : listaLoja.values()){
Log.d("DOIN", loja.getNome() );
}
}
else Log.d("DOIN","NOT WOR");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(listaLojas !=null){
for(SQLloja loja : listaLoja.values()){
Log.d("DOIN2", loja.getNome() );
}
}
else Log.d("DOIN2","NOT WOR");
return listaLojas;*/
}
@Override
protected void onPostExecute(ArrayList<SQLloja> listaLojas){
listaLoja = listaLojas;
for(SQLloja loja : listaLoja){
Log.d("ASYNC24", loja.getNome() );
}
}
}
}
答案 0 :(得分:0)
可能它找不到列名,因为您将表调用为public.lojas_cidadao
。试试这个:
public static final String queryLojasCidadaoOrderByName = "SELECT * FROM public.lojas_cidadao A ORDER BY A.nome_loja_cidadao";
你在其中一行中得到错误:
double lcAltitude = Double.parseDouble(rs.getString(6));
double lcLongitude = Double.parseDouble(rs.getString(7));
可能当您将ORDER BY
返回的数据重新排序时。
换句话说,检查带有和不带ResultSet
子句的ORDER BY
,并检查区别在哪里。祝你好运。
答案 1 :(得分:0)
由于尝试将不代表java.lang.NumberFormatException
的字符串解析为Double
,您获得的是Double
。没有理由说这与PostgreSQL有什么关系。
您可以在申请中解决许多问题:
首先,您似乎期望某些text / char / varchar列始终使用表示双精度或整数的字符串填充。如果您确定这些列号始终具有包含这些类型的值,则这听起来像数据库设计中的错误:PostgreSQL(与大多数其他RDMBS一样)支持类型化列,特别是INTEGER
和{{ 1}}。
如果出于独立原因,您确实需要将这些列保留为text / char / varchar,那么您应该真正捕获这些REAL
,因为它们可能会再次出现,具体取决于您之后在表中插入的内容。
其次,使用NumberFormatException
很少是一个好主意(请参阅this question)。它会导致一般的错误。这样做的原因是它隐藏了您想要检索的列。数据库或表的结构可能会发生变化,与使用它的应用程序无关。假设列顺序强制程序员知道数据库中列顺序的当前状态。
有几种解决方案(您实际上可以同时使用两种解决方案):
SELECT *
语句中明确使用列名称(即SELECT
...):您会发现代码更具可读性。SELECT id, name,
元数据查找该位置的列名称(请参阅this question),或使用使用ResultSet
代替columnLabel
的访问者