我正在尝试插入SQLite数据库,但每次我在模拟器上运行程序时它都会继续关闭。我认为它有我的数据库创建语法,但我已经三次检查它,无法找到我的错误。我能想到的另一件事是,如果我必须在清单中添加一些东西来正确运行SQlite数据库。
以下是我的帮助程序类的代码。
public class dbhelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "swimmers";
public static final String TABLE_SWIMMERS = "sfd table";
public static final String C_ID = "id";
public static final String NAME = "name";
public static final String TEAM = "team";
public static final String NOTES = "notes";
public static final int VERSION = 1;
public dbhelper(Context context)
{
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate (SQLiteDatabase db)
{
String createdb = "create table " + TABLE_SWIMMERS + "(" + C_ID + " integer primary key autoincrement, " + NAME + " text, " + TEAM + " text, " + NOTES + " text); ";
db.execSQL(createdb);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("drop table " + TABLE_SWIMMERS);
onCreate(db);
}
//add new entry
void addSwimmer(Swimmer swimmer){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, swimmer.getname());
values.put(TEAM, swimmer.getteam());
values.put(NOTES, swimmer.getnotes());
db.insert(TABLE_SWIMMERS, null, values);
db.close();
}
//Getting single swimmer
Swimmer getSwimmer(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SWIMMERS, new String[] { C_ID, NAME, TEAM, NOTES}, C_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor !=null)
cursor.moveToFirst();
Swimmer swimmer = new Swimmer (Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), null);
return swimmer;
}
public int updateSwimmer(Swimmer swimmer){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, swimmer.getname());
values.put(TEAM, swimmer.getteam());
values.put(NOTES, swimmer.getnotes());
return db.update(TABLE_SWIMMERS, values, C_ID + " = ?", new String[] { String.valueOf(swimmer.getID()) });
}
//delete single contact
public void deleteSwimmer(Swimmer swimmer){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SWIMMERS, C_ID + " = ?", new String[] { String.valueOf(swimmer.getID()) });
db.close();
}
//get contacts count
public int getSwimmersCount(){
String countQuery = "SELECT * FROM " + TABLE_SWIMMERS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
}
下面是我制作的Swimmer课程,我正在尝试将Swimmer插入数据库。
public class Swimmer {
//private variables
int _id;
String _name;
String _team;
String _notes;
public Swimmer(int id, String name, String team, String notes){
this._id = id;
this._name = name;
this._team = team;
this._notes = notes;
}
public Swimmer(String name, String team, String notes){
this._name = name;
this._team = team;
this._notes = notes;
}
public int getID(){
return this._id;
}
public void setID(int id){
this._id = id;
}
public String getname(){
return this._name;
}
public void setname(String name){
this._name = name;
}
public String getteam(){
return this._team;
}
public void setteam(String team){
this._team = team;
}
public String getnotes(){
return this._notes;
}
public void setnotes(String notes){
this._notes = notes;
}
}
这是我打开并插入数据库的代码。
public class CreateNewSwimmerProfile extends Activity {
private dbhelper db;
private final String TAG = "Create New Profile";
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_sfdmain, menu);
return true;
}
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.i("createProfileActivity", "Intent Text");
setContentView(R.layout.createprofile);
Intent createprofileintent = getIntent();
db = new dbhelper(this);
}
public void btn_CLICK_addswimmer (View w)
{
Log.i("Insert: ", "Inserting ..");
String name,team,notes;
EditText nameIn = (EditText) findViewById(R.id.editText_name);
EditText teamIn = (EditText) findViewById(R.id.editText_team);
EditText notesIn = (EditText) findViewById(R.id.editText_notes);
name = nameIn.getText().toString();
team = teamIn.getText().toString();
notes = notesIn.getText().toString();
Swimmer test = new Swimmer(name, team, notes);
db.addSwimmer(test);
}
public void btn_CLICK_cancel (View b)
{
Log.i(TAG, "start create new profile activity");
Intent intent = new Intent(this, SFDMain.class);
startActivity(intent);
}
}
当我尝试插入游泳运动员时,这是我的错误。
11-27 08:58:36.150: E/AndroidRuntime(643): FATAL EXCEPTION: main
11-27 08:58:36.150: E/AndroidRuntime(643): java.lang.IllegalStateException: Could not execute method of the activity
11-27 08:58:36.150: E/AndroidRuntime(643): at android.view.View$1.onClick(View.java:3591)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.view.View.performClick(View.java:4084)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.view.View$PerformClick.run(View.java:16966)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.os.Handler.handleCallback(Handler.java:615)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.os.Handler.dispatchMessage(Handler.java:92)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.os.Looper.loop(Looper.java:137)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-27 08:58:36.150: E/AndroidRuntime(643): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 08:58:36.150: E/AndroidRuntime(643): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 08:58:36.150: E/AndroidRuntime(643): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-27 08:58:36.150: E/AndroidRuntime(643): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-27 08:58:36.150: E/AndroidRuntime(643): at dalvik.system.NativeStart.main(Native Method)
11-27 08:58:36.150: E/AndroidRuntime(643): Caused by: java.lang.reflect.InvocationTargetException
11-27 08:58:36.150: E/AndroidRuntime(643): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 08:58:36.150: E/AndroidRuntime(643): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.view.View$1.onClick(View.java:3586)
11-27 08:58:36.150: E/AndroidRuntime(643): ... 11 more
11-27 08:58:36.150: E/AndroidRuntime(643): Caused by: android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: create table sfd table(id integer primary key autoincrement, name text, team text, notes text);
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
11-27 08:58:36.150: E/AndroidRuntime(643): at com.sfd.swimming.feedback.display.system.dbhelper.onCreate(dbhelper.java:35)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
11-27 08:58:36.150: E/AndroidRuntime(643): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
11-27 08:58:36.150: E/AndroidRuntime(643): at com.sfd.swimming.feedback.display.system.dbhelper.addSwimmer(dbhelper.java:48)
11-27 08:58:36.150: E/AndroidRuntime(643): at com.sfd.swimming.feedback.display.system.CreateNewSwimmerProfile.btn_CLICK_addswimmer(CreateNewSwimmerProfile.java:50)
11-27 08:58:36.150: E/AndroidRuntime(643): ... 14 more
11-27 08:58:36.420: D/dalvikvm(643): GC_CONCURRENT freed 207K, 4% free 8241K/8519K, paused 22ms+33ms, total 304ms
11-27 08:58:38.680: I/Process(643): Sending signal. PID: 643 SIG: 9
非常感谢任何帮助!!
答案 0 :(得分:0)
table是SQLite的关键字。尝试将变量TABLE_SWIMMERS从TABLE_SWIMMERS = "sfd table";
更改为TABLE_SWIMMERS = "sfd";
,它会起作用。