這是我的問題:我有一個SQLite數據庫設置爲存儲一些值。在這種情況下,它非常簡單。兩列(_id,名稱)。我知道數據正在存儲在數據庫中,我知道我用來傳遞該信息的光標也正在填充。但是,當我嘗試向ArrayList添加遊標值時,出現錯誤。這是我的問題方法的代碼:將光標值傳遞給ArrayList
public void sqltest(LDatabase LConnector){
cursor = LConnector.getAllRecords(); //gets the cursor from my db
try{
cursor.moveToFirst();}
catch (Exception e){
log.i("Failed", "Couldn't moveToFirst"); //I get a successful moveToFirst
}
while(cursor.moveToNext()){
try{
getWork.add(cursor.getString(cursor.getColumnIndex("name")));
} catch (Exception h){
Log.i("FAILED", cursor.getString(cursor.getColumnIndex("name")));
}
}
我設置了最後一個日誌告訴我在那個位置的光標的值。爲數據庫輸入的值總是打印出來,所以我知道光標正在填充。任何人都知道我在這裏做錯了嗎?
編輯: logcat的顯示在此之後這兩行的活動開始時,被稱爲:
04-12 23:26:26.606: I/MOVED(9478): MOVED TO FIRST
04-12 23:26:26.606: I/FAILED(9478): test1
有沒有更好形容它更詳細的錯誤,這就是爲什麼我很困惑。它根本不會被存儲到AssetList中。
這是getAllRecords)的代碼(mehod:
public Cursor getAllLifts() throws SQLiteException
{
open();
return database.rawQuery("SELECT * FROM contacts"+";", null);
}
另外,這裏是創建代碼+代碼用於插入:
創建:
String createQuery = "CREATE TABLE contacts" +
"(_id auto_increment integer primary key," +
"name text);";
db.execSQL(createQuery); // execute the query
插入: open(); //打開數據庫 嘗試{// 添加更多的後來獲得的數據,從USR插入,只是爲了測試 database.execSQL( 「INSERT INTO觸點(名稱)VALUES( 'TEST1')」); database.execSQL( 「INSERT INTO觸點(名稱)VALUES( '測試2')」);}
catch (Exception insert){
Log.i("INSERT", "INSERT FAILED");
}
close(); // close the database
編輯:活動W的休息/進口
package com.jydev.llogger2;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.database.Cursor;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Create extends Activity implements OnClickListener {
Button buttonNext; //continue button
Button buttonBack; //back button
EditText nameEditText; //editText w/ workout name
EditText commentEditText; //editText w/ comments
Intent info; //intent used to pass extras
String wName = null;
String wComments = null;
ArrayList<String> getWork;
Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
commentEditText = (EditText)findViewById(R.id.commentEditText);
buttonBack = (Button)findViewById(R.id.create_back);
buttonNext = (Button)findViewById(R.id.create_continue);
nameEditText = (EditText)findViewById(R.id.nameEditText);
LtDatabase LConnector = new LDatabase(this);
LConnector.insert();
sqltest(LConnector);
}
如果你不告訴我們錯誤是什麼,我們不知道你會得到什麼錯誤。根據請求將您的logcat –
添加到原始q –
發佈您的代碼getAllRecords()和整個logcat。如果「名稱」無效,您只能獲得例外。 –