1

我必须用 50 个美国州及其各自的首都填充 SQLite 表。我是新手,所以我通过以下方式完成了它,但我知道必须有一个更简单的方法:

public void populateDB()
   {
   SQLiteDatabase db = openOrCreateDatabase("mydb.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
   db.setLockingEnabled(true);
   db.setVersion(1);

   final String sqlcmd="create table IF NOT EXISTS gameinfo(_id integer primary key" + 
                         "autoincrement state text not null, autoincrement capital text not null);"; 
   db.execSQL(sqlcmd);
   ContentValues cv = new ContentValues();
   cv.put("state", "Alabama");
   cv.put("captial", "Montgomery");
   cv.put("state", "Alaska");
   cv.put("captial", "Juneau");
   cv.put("state", "Arizona");
   cv.put("captial", "Phoenix");
   cv.put("state", "Arkansas");
   cv.put("captial", "Little Rock");
   cv.put("state", "California");
   cv.put("captial", "Sacramento");
   cv.put("state", "Colorado");
   cv.put("captial", "Denver");
   cv.put("state", "Connecticut");
   cv.put("captial", "Hartford");
   cv.put("state", "Delaware");
   cv.put("captial", "Dover");
   cv.put("state", "Florida");
   cv.put("captial", "Tallahassee");
   cv.put("state", "Georgia");
   cv.put("captial", "Atlanta");
   cv.put("state", "Hawaii");
   cv.put("captial", "Honolulu");
   cv.put("state", "Idaho");
   cv.put("captial", "Boise");
   cv.put("state", "Illinois");
   cv.put("captial", "Springfield");
   cv.put("state", "Indiana");
   cv.put("captial", "Indianapolis");
   cv.put("state", "Iowa");
   cv.put("captial", "Des Moines");
   cv.put("state", "Kansas");
   cv.put("captial", "Topeka");
   cv.put("state", "Kentucky");
   cv.put("captial", "Frankfort");
   cv.put("state", "Louisiana");
   cv.put("captial", "Baton Rouge");
   cv.put("state", "Maine");
   cv.put("captial", "Augusta");
   cv.put("state", "Maryland");
   cv.put("captial", "Annapolis");
   cv.put("state", "Massachusetts");
   cv.put("captial", "Boston");
   cv.put("state", "Michigan");
   cv.put("captial", "Lansing");
   cv.put("state", "Minnesota");
   cv.put("captial", "Saint Paul");
   cv.put("state", "Mississippi");
   cv.put("captial", "Jackson");
   cv.put("state", "Missouri");
   cv.put("captial", "Jefferson City");
   cv.put("state", "Montana");
   cv.put("captial", "Helena");
   cv.put("state", "Nebraska");
   cv.put("captial", "Lincoln");
   cv.put("state", "Nevada");
   cv.put("captial", "Carson City");
   cv.put("state", "New Hampshire");
   cv.put("captial", "Concord");
   cv.put("state", "New Jersey");
   cv.put("captial", "Trenton");
   cv.put("state", "New Mexico");
   cv.put("captial", "Santa Fe");
   cv.put("state", "New York");
   cv.put("captial", "Albany");
   cv.put("state", "North Carolina");
   cv.put("captial", "Raleigh");
   cv.put("state", "North Dakota");
   cv.put("captial", "Bismarck");       
   cv.put("state", "Ohio");
   cv.put("captial", "Columbus");
   cv.put("state", "Oklahoma");
   cv.put("captial", "Oklahoma City");
   cv.put("state", "Oregon");
   cv.put("captial", "Salem");
   cv.put("state", "Pennsylvania");
   cv.put("captial", "Harrisburg");
   cv.put("state", "Rhode Island");
   cv.put("captial", "Providence");
   cv.put("state", "South Carolina");
   cv.put("captial", "Columbia");
   cv.put("state", "South Dakota");
   cv.put("captial", "Pierre");
   cv.put("state", "Tennessee");
   cv.put("captial", "Nashville");
   cv.put("state", "Texas");
   cv.put("captial", "Austin");
   cv.put("state", "Utah");
   cv.put("captial", "Salt Lake City");
   cv.put("state", "Vermont");
   cv.put("captial", "Montpelier");
   cv.put("state", "Virginia");
   cv.put("captial", "Richmond");
   cv.put("state", "Washington");
   cv.put("captial", "Olympia");
   cv.put("state", "West Virginia");
   cv.put("captial", "Charleston");
   cv.put("state", "Wisconsin");
   cv.put("captial", "Madison");
   cv.put("state", "Wyoming");
   cv.put("captial", "Cheyenne");
   long recNum = db.insert("gameinfo", null, cv);                                           
}    
4

1 回答 1

0

您应该创建一个表助手来创建和填充数据库表

public class DictionaryOpenHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
private static final String DICTIONARY_TABLE_NAME = "dictionary";
private static final String DICTIONARY_TABLE_CREATE =
            "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
            KEY_WORD + " TEXT, " +
            KEY_DEFINITION + " TEXT);";

DictionaryOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);        
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DICTIONARY_TABLE_CREATE);
    // db = getWritableDatabase();
    // put your data population code here
}

}

于 2013-07-17T07:33:44.473 回答