从数据库填充微调器

时间:2011-03-23 07:17:32

标签: android database spinner

喜    我试图从DB填充一个微调器......

我的代码:

  String MY_DATABASE_NAME = "testDB"; 
  String MY_DATABASE_TABLE = "testDBrecords"; 
  TextView resultsView; 
  Spinner spinner; 
  ArrayAdapter<String> adapterForSpinner; 

  /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  spinner = (Spinner) findViewById(R.id.spinner); 
  resultsView = (TextView) findViewById(R.id.textview); 

  SQLiteDatabase myDB = null; 
  myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, 1, null); 
  myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DATABASE_TABLE + " (LastName VARCHAR, FirstName VARCHAR, Country VARCHAR, Age INT (3));"); 
  myDB.execSQL("INSERT INTO " + MY_DATABASE_TABLE + " (LastName, FirstName, Country, Age) VALUES ('name1b', 'name1a', 'countrya', 34);"); 
  myDB.execSQL("INSERT INTO " + MY_DATABASE_TABLE + " (LastName,FirstName, Country, Age) VALUES ('name2b, 'name2a, 'countryb', 27);"); 
  Cursor c = myDB.query(MY_DATABASE_TABLE, null, null, null, null, null, null); 
  int firstnameColumnIndex = c.getColumnIndexOrThrow ("FirstName"); 
  int ageColumnIndex = c.getColumnIndexOrThrow("Age"); 
  adapterForSpinner = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); 
          adapterForSpinner.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); 
          spinner.setAdapter(adapterForSpinner); 
  if (c.moveToFirst()) { 
          do { 
                  adapterForSpinner.add(c.getString(firstnameColumnIndex) + " aged " + c.getShort(ageColumnIndex)); 
          } 
          while (c.moveToNext()); 
          if (myDB != null) { 
          myDB.close(); 
      } 
 } 
else { 
  resultsView.setText("DB EMPTY!!"); 
} 
} 

没有错误,但运行时关闭....

logCat错误:

03-24 03:09:31.501: INFO/Database(546): sqlite returned: error code = 1, msg = near "name2a": syntax error
03-24 03:09:31.501: ERROR/Database(546): Failure 1 (near "name2a": syntax error) on 0x1ec430 when preparing 'INSERT INTO testDBrecords (LastName,FirstName, Country, Age) VALUES ('name2b, 'name2a, 'countryb', 27);'.
03-24 03:09:31.743: DEBUG/AndroidRuntime(546): Shutting down VM
03-24 03:09:31.743: WARN/dalvikvm(546): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-24 03:09:31.981: ERROR/AndroidRuntime(546): FATAL EXCEPTION: main
03-24 03:09:31.981: ERROR/AndroidRuntime(546): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acarin.project/com.acarin.project.Rough}: android.database.sqlite.SQLiteException: near "name2a": syntax error: INSERT INTO testDBrecords (LastName,FirstName, Country, Age) VALUES ('name2b, 'name2a, 'countryb', 27);
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.os.Looper.loop(Looper.java:123)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.app.ActivityThread.main(ActivityThread.java:3647)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at java.lang.reflect.Method.invoke(Method.java:507)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at dalvik.system.NativeStart.main(Native Method)
03-24 03:09:31.981: ERROR/AndroidRuntime(546): Caused by: android.database.sqlite.SQLiteException: near "name2a": syntax error: INSERT INTO testDBrecords (LastName,FirstName, Country, Age) VALUES ('name2b, 'name2a, 'countryb', 27);
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at com.acarin.project.Rough.onCreate(Rough.java:32)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
03-24 03:09:31.981: ERROR/AndroidRuntime(546):     ... 11 more
03-24 03:09:32.178: WARN/ActivityManager(66):   Force finishing activity com.acarin.project/.Rough
03-24 03:09:32.767: WARN/ActivityManager(66): Activity pause timeout for HistoryRecord{406c9448 com.acarin.project/.Rough}
03-24 03:09:33.693: WARN/ActivityManager(66): Activity idle timeout for HistoryRecord{406c9448 com.acarin.project/.Rough}
03-24 03:09:37.055: DEBUG/dalvikvm(282): GC_EXPLICIT freed 19K, 55% free 2594K/5703K, external 716K/1038K, paused 15976ms
03-24 03:09:41.994: DEBUG/dalvikvm(299): GC_EXPLICIT freed 6K, 54% free 2536K/5511K, external 716K/1038K, paused 317ms
03-24 03:09:43.742: WARN/ActivityManager(66): Activity destroy timeout for HistoryRecord{406c9448 com.acarin.project/.Rough}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

  

myDB.execSQL(“INSERT INTO”+   MY_DATABASE_TABLE +“   (LastName,FirstName,Country,Age)   VALUES('name2b,'name2a ,'countryb',   27);“);

你错过了两个引号!将其更改为:

  

myDB.execSQL(“INSERT INTO”+   MY_DATABASE_TABLE +“   (LastName,FirstName,Country,Age)   价值观('name2b','name2a',   'countryb',27);“);