我在数据库文件中遇到上述错误。我是编码的新手。请帮我解决这个问题。我不知道这些类型的问题。请给我建议。
我的数据库代码如下所示:
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper{
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "applicationDB";
// Employee table name
private static final String TABLE_USERS = "apps";
private static String SlNo = "slno";
private static String NAME = "name";
private static String PHONE = "phone";
private static String EMAIL = "email";
private static String ADDRESS = "address";
private static String COMMENTS = "comments";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USER_TABLE = " CREATE TABLE " + TABLE_USERS + "("
+ SlNo + " INTEGER PRIMARY KEY, "
+ NAME + " TEXT, "
+ PHONE + " TEXT, "
+ EMAIL + " TEXT, "
+ ADDRESS + " TEXT, "
+ COMMENTS + " TEXT "
+ ")";
db.execSQL(CREATE_USER_TABLE);
//db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
Log.e(NAME, "Error is here");
Log.e(PHONE, "Error is here");
Log.e(EMAIL, "Error is here");
Log.e(ADDRESS, "Error is here");
Log.e(COMMENTS, "Error is here");
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_USERS);
// Create tables again
onCreate(db);
}
// Adding new user
public int addUser(User user)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, user.getName());
values.put(ADDRESS, user.getAddress());
values.put(PHONE, user.getPhone());
values.put(EMAIL, user.getEmail());
values.put(COMMENTS, user.getComments());
// Inserting Row
int id = (int) db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
return id;
}
//updating a existing user
public int editUser(User user, String position) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, user.getName());
values.put(ADDRESS, user.getAddress());
values.put(PHONE, user.getPhone());
values.put(EMAIL, user.getEmail());
values.put(COMMENTS, user.getComments());
// updating row
return db.update(TABLE_USERS, values, SlNo + " = ?",
new String[] { String.valueOf(position) });
}
public User getUser(String id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS, new String[] {
SlNo, NAME, ADDRESS, PHONE, EMAIL, COMMENTS }, SlNo + "=?",
new String[] {String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
User user = new User(cursor.getString(1),
cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
db.close();
// return user
return user;
}
// LIst all users
public String[] getAllUsers() {
ArrayList<String> usersList = new ArrayList<String>();
Log.e(NAME, "Error is here");
Log.e(PHONE, "Error is here");
Log.e(EMAIL, "Error is here");
Log.e(ADDRESS, "Error is here");
Log.e(COMMENTS, "Error is here");
String selectQuery = " SELECT * FROM " + TABLE_USERS;
try {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
String user = cursor.getString(1) + ", " + cursor.getString(2) + ", " + cursor.getString(3) + ", " + cursor.getString(4) + ", " + cursor.getString(5);
usersList.add(user);
} while (cursor.moveToNext());
}
String[] users = new String[usersList.size()];
db.close();
return (usersList.toArray(users));
}
catch(Exception e) {
Log.d("Error in getting users from DB", e.getMessage());
return null;
}
}
}
我观察到的logcat痕迹如下所示:
12-23 04:12:29.037: D/value of position from intent(3072): null
12-23 04:12:29.407: D/AndroidRuntime(3072): Shutting down VM
12-23 04:12:29.427: W/dalvikvm(3072): threadid=1: thread exiting with uncaught exception (group=0xb0f5e648)
12-23 04:12:29.487: E/AndroidRuntime(3072): FATAL EXCEPTION: main
12-23 04:12:29.487: E/AndroidRuntime(3072): java.lang.RuntimeException: Unable to start activity ComponentInfo{form.app.applicationform1/form.app.applicationform1.DetailsUsers}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.os.Looper.loop(Looper.java:137)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-23 04:12:29.487: E/AndroidRuntime(3072): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 04:12:29.487: E/AndroidRuntime(3072): at java.lang.reflect.Method.invoke(Method.java:525)
12-23 04:12:29.487: E/AndroidRuntime(3072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-23 04:12:29.487: E/AndroidRuntime(3072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-23 04:12:29.487: E/AndroidRuntime(3072): at dalvik.system.NativeStart.main(Native Method)
12-23 04:12:29.487: E/AndroidRuntime(3072): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
12-23 04:12:29.487: E/AndroidRuntime(3072): at form.app.applicationform1.DatabaseHandler.getUser(DatabaseHandler.java:97)
12-23 04:12:29.487: E/AndroidRuntime(3072): at form.app.applicationform1.DetailsUsers.onCreate(DetailsUsers.java:42)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.Activity.performCreate(Activity.java:5133)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-23 04:12:29.487: E/AndroidRuntime(3072): ... 11 more
12-23 04:14:59.689: I/Process(3072): Sending signal. PID: 3072 SIG: 9
请帮帮我。
答案 0 :(得分:2)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
您的光标不包含任何数据
您需要像这样验证Cursor
。如果您的光标数大于0,那么只需要从光标中获取数据。
public User getUser(String id) {
User mUser = null;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS, new String[] { SlNo, NAME, ADDRESS, PHONE, EMAIL, COMMENTS }, SlNo + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null && cursor.getCount > 0) {
cursor.moveToFirst();
mUser = new User(cursor.getString(cursor.getColumnIndex("NAME")),
cursor.getString(cursor.getColumnIndex("ADDRESS")),
cursor.getString(cursor.getColumnIndex("PHONE")),
cursor.getString(cursor.getColumnIndex("EMAIL")),
cursor.getString(cursor.getColumnIndex("COMMENTS")));
cursor.close();
}
d.close();
retunr mUser;
}
答案 1 :(得分:1)
问题在于您的getUser
功能。
也许您的cursor
不为空,但它仍然可能为空:
尝试以下方法:
public User getUser(String id) {
User user = null;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS, new String[] {
SlNo, NAME, ADDRESS, PHONE, EMAIL, COMMENTS }, SlNo + "=?",
new String[] {String.valueOf(id)}, null, null, null, null);
if (cursor != null)
{
if(cursor.moveToFirst())
{
user = new User(cursor.getString(1),
cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
}
}
db.close();
// return user
return user;
}
希望有所帮助
答案 2 :(得分:1)
首先检查是否存在数据
if (cursor != null && cursor.getCount > 0) {
}
然后需要从光标的 th 位置读取数据
更改此
User user = new User(cursor.getString(1),
cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
到
User user = new User(cursor.getString(0),cursor.getString(1),
cursor.getString(2), cursor.getString(3), cursor.getString(4));