朋友们,我已经创建了一个数据库。 现在我想在其中添加一个新事件,但通过" INSERT INTO" 我不想使用" contentvalues"。
这是我的代码部分 提前谢谢。
protected static final int DATABASE_VERSION = 1;
protected static final String DATABSE_NAME = "eventDB.db";
protected static final String TABLE_EVENTS = "Events";
public static final String COLUMN_ID = "id";
public static final String COLUMN_EVENTNAME = "eventname";
String insertQuery = " INSERT INTO " + TABLE_EVENTS + "("
+ COLUMN_EVENTNAME + ")" + " VALUES ( ' "
+ eventname + " ' )";
mDatabase.execSQL(insertQuery);
这是错误日志
09-21 15:20:15.992: E/AndroidRuntime(828): java.lang.IllegalStateException: Could not execute method of the activity
09-21 15:20:15.992: E/AndroidRuntime(828): at android.view.View$1.onClick(View.java:3633)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.view.View.performClick(View.java:4240)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.view.View$PerformClick.run(View.java:17721)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.os.Handler.handleCallback(Handler.java:730)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:92)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:137)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-21 15:20:15.992: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 15:20:15.992: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:525)
09-21 15:20:15.992: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-21 15:20:15.992: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-21 15:20:15.992: E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native Method)
09-21 15:20:15.992: E/AndroidRuntime(828): Caused by: java.lang.reflect.InvocationTargetException
09-21 15:20:15.992: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 15:20:15.992: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:525)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.view.View$1.onClick(View.java:3628)
09-21 15:20:15.992: E/AndroidRuntime(828): ... 11 more
09-21 15:20:15.992: E/AndroidRuntime(828): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
09-21 15:20:15.992: E/AndroidRuntime(828): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
09-21 15:20:15.992: E/AndroidRuntime(828): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
09-21 15:20:15.992: E/AndroidRuntime(828): at com.database.DBManager.cursorToEvent(DBManager.java:96)
09-21 15:20:15.992: E/AndroidRuntime(828): at com.database.DBManager.addEvent(DBManager.java:55)
09-21 15:20:15.992: E/AndroidRuntime(828): at com.database.MainActivity.addClick(MainActivity.java:55)
09-21 15:20:15.992: E/AndroidRuntime(828): ... 14 more
09-21 15:38:20.721: E/AndroidRuntime(887): FATAL EXCEPTION: main
09-21 15:38:20.721: E/AndroidRuntime(887): java.lang.IllegalStateException: Could not execute method of the activity
09-21 15:38:20.721: E/AndroidRuntime(887): at android.view.View$1.onClick(View.java:3633)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.view.View.performClick(View.java:4240)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.view.View$PerformClick.run(View.java:17721)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.os.Handler.handleCallback(Handler.java:730)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.os.Handler.dispatchMessage(Handler.java:92)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.os.Looper.loop(Looper.java:137)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-21 15:38:20.721: E/AndroidRuntime(887): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 15:38:20.721: E/AndroidRuntime(887): at java.lang.reflect.Method.invoke(Method.java:525)
09-21 15:38:20.721: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-21 15:38:20.721: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-21 15:38:20.721: E/AndroidRuntime(887): at dalvik.system.NativeStart.main(Native Method)
09-21 15:38:20.721: E/AndroidRuntime(887): Caused by: java.lang.reflect.InvocationTargetException
09-21 15:38:20.721: E/AndroidRuntime(887): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 15:38:20.721: E/AndroidRuntime(887): at java.lang.reflect.Method.invoke(Method.java:525)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.view.View$1.onClick(View.java:3628)
09-21 15:38:20.721: E/AndroidRuntime(887): ... 11 more
09-21 15:38:20.721: E/AndroidRuntime(887): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
09-21 15:38:20.721: E/AndroidRuntime(887): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
09-21 15:38:20.721: E/AndroidRuntime(887): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
09-21 15:38:20.721: E/AndroidRuntime(887): at com.database.DBManager.cursorToEvent(DBManager.java:96)
09-21 15:38:20.721: E/AndroidRuntime(887): at com.database.DBManager.addEvent(DBManager.java:55)
09-21 15:38:20.721: E/AndroidRuntime(887): at com.database.MainActivity.addClick(MainActivity.java:55)
09-21 15:38:20.721: E/AndroidRuntime(887): ... 14 more
我的MainActivity.java
package com.database;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import com.ajay.database.R;
public class MainActivity extends Activity {
private DBManager mEvemanager;
private EditText mName;
public CustomListAdapter mAdapter;
public ArrayList<Event> mEvents;
public ListView mListview;
public String TAG = "MyActivityTag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Attendance Tracker");
mEvemanager = new DBManager(this);
// Log.d(TAG, "Back into main after creating object of eventmanager");
mEvemanager.open();
mEvents = mEvemanager.getallEvents();
// Log.d(TAG,
// "Back into main activity after got events from eventmanager");
mAdapter = new CustomListAdapter(MainActivity.this, mEvents);
// Log.d(TAG, "Back into main after creating customadapter");
mListview = (ListView) findViewById(R.id.listview);
// Log.d(TAG, "listView reference Created");
mListview.setAdapter(mAdapter);
// Log.d(TAG, "Adapter Set");
}
public void addClick(View view) {
Log.d(TAG, "Add button clicked");
Event event = null;
mName = (EditText) findViewById(R.id.eventNameText);
event = mEvemanager.addEvent(mName.getText().toString());
mAdapter.add(event);
mAdapter.notifyDataSetChanged();
mName.setText(null);
}
@Override
protected void onResume() {
mEvemanager.open();
super.onResume();
}
@Override
protected void onPause() {
mEvemanager.close();
super.onPause();
}
}
我的DBManager.java
package com.database;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DBManager {
private SQLiteDatabase mDatabase;
private MyDBHandler mDBhandler;
private String[] allEvents = { MyDBHandler.COLUMN_ID,
MyDBHandler.COLUMN_EVENTNAME };
public static final String TAG = "MyActivityTag";
public DBManager(Context context) {
// Log.d(TAG, "Eventmanager constructor called");
mDBhandler = new MyDBHandler(context);
}
public void open() throws SQLException {
mDatabase = mDBhandler.getWritableDatabase();
}
public void close() throws SQLException {
mDBhandler.close();
}
public Event addEvent(String eventname) {
Log.d(TAG, "addEvent method in eventmanager");
// ContentValues values = new ContentValues();
// values.put(MyDBHandler.COLUMN_EVENTNAME, eventname);
String insertQuery = " INSERT INTO " + MyDBHandler.TABLE_EVENTS + "("
+ MyDBHandler.COLUMN_EVENTNAME + ")" + " VALUES ( ' "
+ eventname + " ' )";
mDatabase.execSQL(insertQuery);
// long insertId = mDatabase
// .insert(MyDBHandler.TABLE_EVENTS, null, values);
Cursor cursor = mDatabase.query(MyDBHandler.TABLE_EVENTS, allEvents,
MyDBHandler.COLUMN_ID + " = " + 0, null, null, null, null);
cursor.moveToFirst();
Event mEvent = cursorToEvent(cursor);
cursor.close();
return mEvent;
}
public void deleteEvent(String eventname) {
Log.d(TAG, "in deleteEvent method eventmanager");
String query = "Select * FROM " + MyDBHandler.TABLE_EVENTS + " WHERE "
+ MyDBHandler.COLUMN_EVENTNAME + " = \"" + eventname + "\"";
SQLiteDatabase db = mDBhandler.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Event event = new Event();
if (cursor.moveToFirst()) {
event.setID(Integer.parseInt(cursor.getString(0)));
db.delete(MyDBHandler.TABLE_EVENTS, MyDBHandler.COLUMN_ID + " = ?",
new String[] { String.valueOf(event.getID()) });
cursor.close();
}
db.close();
}
public ArrayList<Event> getallEvents() {
Log.d(TAG, "getallEvents in eventmanager");
ArrayList<Event> events = new ArrayList<Event>();
Cursor cursor = mDatabase.query(MyDBHandler.TABLE_EVENTS, allEvents,
null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Event event = cursorToEvent(cursor);
events.add(event);
cursor.moveToNext();
}
cursor.close();
return events;
}
private Event cursorToEvent(Cursor cursor) {
Event event = new Event();
event.setID(cursor.getInt(0));
event.setEventName(cursor.getString(1));
return event;
}
}
和MyDBHandler.java
package com.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MyDBHandler extends SQLiteOpenHelper {
protected static final int DATABASE_VERSION = 1;
protected static final String DATABSE_NAME = "eventDB.db";
protected static final String TABLE_EVENTS = "Events";
public static final String COLUMN_ID = "id";
public static final String COLUMN_EVENTNAME = "eventname";
public static final String TAG = "MyActivityTag";
public MyDBHandler(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABSE_NAME, factory, DATABASE_VERSION);
// Log.d(TAG, "MyDBHandler constructor called with 4 arguments");
}
public MyDBHandler(Context context) {
super(context, DATABSE_NAME, null, DATABASE_VERSION);
// Log.d(TAG, "MyDBHandler const called with only context argument");
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_EVENTS_TABLE = " CREATE TABLE " + TABLE_EVENTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_EVENTNAME
+ " TEXT" + ")";
db.execSQL(CREATE_EVENTS_TABLE);
Log.d(TAG, "Table Created in database");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);
onCreate(db);
}
}
答案 0 :(得分:0)
最可能的原因是数据库中的Event表中没有ID为0的行。因此,您的光标为空,并在cursorToEvent