强大的文字我正在使用eclipse和android sdk r18做一些数据库操作:
我有一个名为“advance_search”的视图,并存储一些有关请求调用的数据。
现在我想使用简单的rawquery函数从数据库中查出一些记录。 但我失败了!不知道为什么!!
任何人都可以给我一些建议吗?
日志:
05-11 06:37:11.057: E/Status(1306): Database Initialize OK!
05-11 06:37:11.098: E/Status(1306): Starting Database Check procedure
05-11 06:37:11.098: E/Status(1306): Database already exist!
05-11 06:37:11.117: E/Status(1306): Open Database OK
05-11 06:37:11.117: E/LogSQLProcess(1306): String id is :6326
05-11 06:37:11.138: E/LogSQLProcess(1306): SQLiteDatabase in READ MODE!
05-11 06:37:11.148: E/LogSQLProcess(1306): DB Query Logon_info View complete!
05-11 06:37:11.148: E/LogSQLProcess(1306): Cursor moved to Position 0!
05-11 06:37:11.148: E/LogSQLProcess(1306): USER Name is John
05-11 06:37:11.168: D/AndroidRuntime(1306): Shutting down VM
05-11 06:37:11.168: W/dalvikvm(1306): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-11 06:37:11.178: E/AndroidRuntime(1306): FATAL EXCEPTION: main
05-11 06:37:11.178: E/AndroidRuntime(1306): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TicketDroid.PKG/com.TicketDroid.PKG.User_Search}: java.lang.ArrayIndexOutOfBoundsException
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.os.Looper.loop(Looper.java:123)
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-11 06:37:11.178: E/AndroidRuntime(1306): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 06:37:11.178: E/AndroidRuntime(1306): at java.lang.reflect.Method.invoke(Method.java:507)
05-11 06:37:11.178: E/AndroidRuntime(1306): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-11 06:37:11.178: E/AndroidRuntime(1306): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-11 06:37:11.178: E/AndroidRuntime(1306): at dalvik.system.NativeStart.main(Native Method)
05-11 06:37:11.178: E/AndroidRuntime(1306): Caused by: java.lang.ArrayIndexOutOfBoundsException
05-11 06:37:11.178: E/AndroidRuntime(1306): at com.TicketDroid.PKG.User_Search.onCreate(User_Search.java:105)
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 06:37:11.178: E/AndroidRuntime(1306): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-11 06:37:11.178: E/AndroidRuntime(1306): ... 11 more
com.TicketDroid.PKG / com.TicketDroid.PKG.User_Search是MY JAVA文件
数据库操作代码:
public Cursor UserSearch(String id) {
// TODO Auto-generated method stub
Log.e("LogSQLProcess", "String id is :"+id);
SQLiteDatabase dbs = this.getReadableDatabase();
Log.e("LogSQLProcess", "SQLiteDatabase in READ MODE!");
String selectionArgs[] = new String[] {id};
Cursor n=dbs.rawQuery("SELECT * FROM logon_info WHERE _id= ?", selectionArgs);
Log.e("LogSQLProcess", "DB Query Logon_info View complete!");
n.moveToPosition(0);
Log.e("LogSQLProcess", "Cursor moved to Position 0!");
String name=n.getString(0);
Log.e("LogSQLProcess", "USER Name is "+name);
String Args[] = new String[] {name};
return dbs.rawQuery("select * from advance_search where REQUEST_PERSON= ?", Args);
我已经测试了sqlite3中的sql语句NAD肯定是可行的!
很抱歉给您带来不便!
以下是完整代码:
DBInitialize.java
public class DB_Initialize extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.TicketDroid.PKG/databases/";
private static String DB_NAME = "TicketDroid";
private static String DB = "TicketDroid";
private SQLiteDatabase myDataBase;
private final Context myContext;
public static String LOGON_TABLE = "LOGON";
public static String LOGON_VIEW = "logon_info";
public static String TICKET_VIEW = "advance_search";
public DB_Initialize(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
Log.e("Status", "Database Initialize OK!");
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
this.getReadableDatabase();
Log.e("Status", "Create Database in REAED MODE");
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String Path = DB_PATH + DB;
checkDB = SQLiteDatabase.openDatabase(Path, null, SQLiteDatabase.OPEN_READONLY);
Log.e("Status", "Starting Database Check procedure");
}catch(SQLiteException e){
Log.e("Status", "Database doesn't exist!");
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
Log.e("Status", "Database already exist!");
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
Log.e(" Status :","Prepare to copy database!" );
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
Log.e("Status", "Copy Database file complete");
//Close the streams
Log.e(" Status :","Prepare to Close file stream!" );
myOutput.flush();
myOutput.close();
myInput.close();
Log.e("Status", "Closed file stream!");
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Log.e("Status", "Open Database OK");
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
Log.e("Status", "Database closed");
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor UserSearch(String id) {
// TODO Auto-generated method stub
Log.e("LogSQLProcess", "String id is :"+id);
SQLiteDatabase dbs = this.getReadableDatabase();
Log.e("LogSQLProcess", "SQLiteDatabase in READ MODE!");
String selectionArgs[] = new String[] {id};
Cursor n=dbs.rawQuery("SELECT * FROM logon_info WHERE _id= ?", selectionArgs);
Log.e("LogSQLProcess", "DB Query Logon_info View complete!");
n.moveToPosition(0);
Log.e("LogSQLProcess", "Cursor moved to Position 0!");
String name=n.getString(0);
Log.e("LogSQLProcess", "USER Name is "+name);
n.close();
String Args[] = new String[] {name};
return dbs.rawQuery("select * from advance_search where REQUEST_PERSON= ?", Args); }
User_search.java
public class User_Search extends Activity implements OnClickListener {
Button srhu_back,srhu_next,srhu_previous;
TextView tv_result;
String Password;
String Type;
String ID;
Integer count=0;
Cursor srh = null;
ArrayList<String> array=new ArrayList<String>();
String str[]={};
String strg="";
String[] rset=new String[] {
"REQUEST_PERSON","USER_EXT",
"DEPARTMENT_CODE","EMAIL_ADDR",
"_id","TICKET_Title",
"PROBLEM_SOFT","CATEGORY",
"SYMPTONS","CONTACT",
"STATUS","FINISH_DATE","ACTIONS",
"ENGINEER1","ENGINEER1_EXT",
"ENGINEER1","ENGINEER1_EXT",
};
String res = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_search);
srhu_back = (Button) findViewById(R.id.srhu_bBack);
srhu_next = (Button) findViewById(R.id.srhu_bNext);
srhu_previous = (Button) findViewById(R.id.srhu_bPrevious);
tv_result = (TextView) findViewById(R.id.srhu_tvresult);
srhu_back.setOnClickListener(this);
srhu_next.setOnClickListener(this);
srhu_previous.setOnClickListener(this);
Bundle bundle = getIntent().getExtras();
Password=bundle.getString("Password");
Type=bundle.getString("Type");
ID=bundle.getString("ID");
DB_Initialize srh_ret = new DB_Initialize(User_Search.this);
try {
srh_ret.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
srh_ret.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
try {
srh=srh_ret.UserSearch(ID);
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(User_Search.this, "Cannot Fetch any records!", Toast.LENGTH_LONG).show();
} finally {
for(srh.moveToFirst(); srh.moveToNext(); srh.isAfterLast()) {
// The Cursor is now set to the right position
for(int i=0; i<=16; i++) {
if (srh.isNull(i)) strg="N/A";
else strg=srh.getString(i);
res = res+rset[i]+ " : "+strg+"\n";
Log.e("TextString","result is "+res+"\n");
}
array.add(res);
}
}
tv_result.setText(str[count]);
count++;
Toast.makeText(User_Search.this, "Record Searching Successful!", Toast.LENGTH_LONG).show(); }
日志:
05-11 09:18:29.807: D/dalvikvm(370): GC_EXTERNAL_ALLOC freed 50K, 53% free 2553K/5379K, external 1625K/2137K, paused 60ms
05-11 09:18:35.807: W/KeyCharacterMap(370): No keyboard for id 0
05-11 09:18:35.807: W/KeyCharacterMap(370): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-11 09:18:39.187: E/Status(370): Database Initialize OK!
05-11 09:18:39.237: E/Status(370): Starting Database Check procedure
05-11 09:18:39.247: E/Status(370): Database already exist!
05-11 09:18:39.268: E/Status(370): Open Database OK
05-11 09:18:39.287: E/LogSQLProcess(370): SQLiteDatabase in READ MODE!
05-11 09:18:39.287: E/LogSQLProcess(370): SQLiteQueryBuilder created!
05-11 09:18:39.297: E/LogSQLProcess(370): SQLiteQueryBuilder set Table!
05-11 09:18:39.297: E/LogSQLProcess 0(370): John
05-11 09:18:39.297: E/LogSQLProcess 1(370): john
05-11 09:18:39.306: E/LogSQLProcess 2(370): USER
05-11 09:18:39.306: E/LogSQLProcess 3(370): 6326
05-11 09:18:39.306: V/CursorValues(370): Johnjohn
05-11 09:18:39.306: V/CursorValues After Moved to First(370): Johnjohn
05-11 09:18:39.306: V/Values(370): John/john
05-11 09:18:39.306: V/CursorValues Now(370): Johnjohn
05-11 09:18:39.306: E/Check Logon Process(370): user and password entered correct!
05-11 09:18:39.306: E/Check Logon Process(370): JohnjohnUSER6326
05-11 09:18:39.326: E/Status(370): Database closed
05-11 09:18:39.337: V/USER TYPE SELECT :(370): USER
05-11 09:18:41.927: E/Status(370): Database Initialize OK!
05-11 09:18:41.946: E/Status(370): Starting Database Check procedure
05-11 09:18:41.946: E/Status(370): Database already exist!
05-11 09:18:41.966: E/Status(370): Open Database OK
05-11 09:18:41.966: E/LogSQLProcess(370): String id is :6326
05-11 09:18:41.997: E/LogSQLProcess(370): SQLiteDatabase in READ MODE!
05-11 09:18:41.997: E/LogSQLProcess(370): DB Query Logon_info View complete!
05-11 09:18:41.997: E/LogSQLProcess(370): Cursor moved to Position 0!
05-11 09:18:41.997: E/LogSQLProcess(370): USER Name is John
05-11 09:18:42.018: D/AndroidRuntime(370): Shutting down VM
05-11 09:18:42.018: W/dalvikvm(370): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-11 09:18:42.027: E/AndroidRuntime(370): FATAL EXCEPTION: main
05-11 09:18:42.027: E/AndroidRuntime(370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TicketDroid.PKG/com.TicketDroid.PKG.User_Search}: java.lang.ArrayIndexOutOfBoundsException
05-11 09:18:42.027: E/AndroidRuntime(370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-11 09:18:42.027: E/AndroidRuntime(370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-11 09:18:42.027: E/AndroidRuntime(370): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-11 09:18:42.027: E/AndroidRuntime(370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-11 09:18:42.027: E/AndroidRuntime(370): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 09:18:42.027: E/AndroidRuntime(370): at android.os.Looper.loop(Looper.java:123)
05-11 09:18:42.027: E/AndroidRuntime(370): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-11 09:18:42.027: E/AndroidRuntime(370): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 09:18:42.027: E/AndroidRuntime(370): at java.lang.reflect.Method.invoke(Method.java:507)
05-11 09:18:42.027: E/AndroidRuntime(370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-11 09:18:42.027: E/AndroidRuntime(370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-11 09:18:42.027: E/AndroidRuntime(370): at dalvik.system.NativeStart.main(Native Method)
05-11 09:18:42.027: E/AndroidRuntime(370): Caused by: java.lang.ArrayIndexOutOfBoundsException
05-11 09:18:42.027: E/AndroidRuntime(370): at com.TicketDroid.PKG.User_Search.onCreate(User_Search.java:122)
05-11 09:18:42.027: E/AndroidRuntime(370): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 09:18:42.027: E/AndroidRuntime(370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-11 09:18:42.027: E/AndroidRuntime(370): ... 11 more
05-11 09:18:46.587: I/Process(370): Sending signal. PID: 370 SIG: 9
答案 0 :(得分:0)
引起:java.lang.ArrayIndexOutOfBoundsException
你的onCreate方法中的。
在执行n.moveToPosition(0);
之前先检查它是否为空......请执行
if(n.moveToFirst()==true) {
String name = n.getString(n.getColumnIndex(<column name>));
}
答案 1 :(得分:0)
为什么不调试应用程序,以便在遇到异常时Eclipse将中断。然后你可以查看所有变量和堆栈调用,看看你错过了什么。这是你代码中的某个地方。