我有一個數據庫(SQLite),其中包含一個帶GPS數據的tabel:經度和緯度!我確信數據庫存在幷包含數據,因爲我已經成功地獲得了遍及數據的光標,而且我已經將其顯示在屏幕上。獲取遊標時出現問題!
現在,我的應用程序應該比寫入數據庫和顯示數據大一點。我應該通過套接字將數據發送到另一個應用程序。
爲此,我創建了另一個線程,它連接到另一個應用程序並打開數據庫並讀取數據並將其發送到另一個應用程序。
public class screen1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
Thread cThread=new Thread(new ClientThread());
cThread.start();
db=new DBAdapter(this);
}
這裏是我的ClientThread類;
public class ClientThread implements Runnable{
private PrintWriter out=null;
public void run()
{
try
{
InetAddress serverAddr=InetAddress.getByName(serverIpAddress);
socket=new Socket(serverAddr,ClientPort);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
Log.d(" ","Clientul is connected");
}
catch(UnknownHostException e){
System.err.println("Don't know about host");
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to host");
}
try{
db.createDatabase();
db.openDataBase();
Cursor c=db.getAllData();
Log.d(" ","before the cursor");
if(c.moveToFirst())
{
do{
Log.d(" ","after the cursor");
longitude=c.getString(1);
latitude=c.getString(2);
Log.d(longitude,latitude);
}while(c.moveToNext());
}
socket.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
現在我面臨的問題是這樣的一個 - 我試圖讓我的經度和緯度顯示,但我的計劃從來沒有達到進入這個循環:
if(c.moveToFirst())
{
do{
Log.d(" ","after the cursor");
longitude=c.getString(1);
latitude=c.getString(2);
Log.d(longitude,latitude);
}while(c.moveToNext());
}
我怎麼樣經過測試? .... =我使用過的消息如:Log.d(" ","before the cursor")
- 在該循環之前以及Log.d(" ","after the cursor")
。
現在第一個總是顯示,但第二個永遠不會顯示。
是否有人知道問題是什麼?因爲如果我試圖在這個線程之外的數據上獲得遊標,那麼所有的工作都很好 - 數據被顯示....並且我也在做同樣的事情!
另一件事是,如果我使用的語句if(!c.moveToFirst())
程序進入循環並顯示在第二個消息buut我在logcat中得到這個錯誤:
04-19 07:51:31.450: ERROR/AndroidRuntime(611): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-19 07:51:31.450: ERROR/AndroidRuntime(611): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
04-19 07:51:31.450: ERROR/AndroidRuntime(611): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
04-19 07:51:31.450: ERROR/AndroidRuntime(611): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
04-19 07:51:31.450: ERROR/AndroidRuntime(611): at test.android.screen1$ClientThread.run(screen1.java:96)
04-19 07:51:31.450: ERROR/AndroidRuntime(611): at java.lang.Thread.run(Thread.java:1096)
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
..What是......? ........因此,在那個光標是我最明確的唯一問題,但什麼?
提前感謝您的有用答覆!!!我在這裏瞭解更多詳情!
UPDATE:
公共類將對DBAdapter擴展SQLiteOpenHelper {
public static final String DATABASE_PATH = "data/data/test.android/database";
public static final String DATABASE_NAME = "GPSdata";
public static final String DATABASE_TABLE_1= "route";
public static final String DATABASE_TABLE_2= "car1_data";
public static final String KEY_ROWID_1= "_id";
public static final String KEY_NAMEOFCAR="name_of_car";
public static final String KEY_ROWID_2 = "_id";
public static final String KEY_LONGITUDE= "longitude";
public static final String KEY_LATITUDE = "latitude";
public static final String KEY_ROUTE= "route";
public SQLiteDatabase db;
private final Context myContext;
public DBAdapter(Context context) {
super(context, DATABASE_NAME, null, 1);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// check if exists and copy database from resource
createDB();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("SqlHelper", "Upgrading database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
onCreate(db);
}
public void createDatabase() {
createDB();
}
private void createDB() {
boolean dbExist = DBExists();
if (!dbExist) {
copyDBFromResource();
}
}
private boolean DBExists() {
SQLiteDatabase db = null;
try {
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null) {
db.close();
}
return db != null ? true : false;
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try {
inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
} catch (IOException e) {
throw new Error("Problem copying database from resource file.");
}
}
public void openDataBase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
public long insertData1(String name_of_car) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAMEOFCAR, name_of_car);
return db.insert(DATABASE_TABLE_1, null, initialValues);
}
public long insertData2(int longitude, int latitude, String route) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_LONGITUDE, longitude);
initialValues.put(KEY_LATITUDE, latitude);
initialValues.put(KEY_ROUTE, route);
return db.insert(DATABASE_TABLE_2, null, initialValues);
}
public Cursor getAllData()
{
return db.query(DATABASE_TABLE_2, new String[] {KEY_ROWID_2,KEY_LONGITUDE,KEY_LATITUDE},null,null,null,null,null);
}
public void clearSelections() {
ContentValues values = new ContentValues();
values.put(" selected", 0);
this.db.update(DBAdapter.DATABASE_TABLE_2, values, null, null);
}
}
沒有人有不小的想法,爲什麼沒有程序進入循環? – adrian 2011-04-19 20:35:44
你的光標是空的。但是我們怎麼能猜到爲什麼?考慮分享你的'db.createDatabase()','db.openDataBase()'和'db.getAllData()'代碼。 – ernazm 2011-04-19 21:00:57
我用所有的方法更新了我的答案......但正如我所說的,當我不使用不同的線程來顯示數據時,光標工作得很好......它真的......。 – adrian 2011-04-19 21:34:48