我的应用程序在启动时将16条记录插入数据库。但是当我输出这些记录时,1条记录(Union Restaurant)正在重复16次。 RLocationDatabase.java
创建数据库。 RDatabaseModel.java
是数据库内容的结构。 MainActivity.java
执行应用程序并插入记录。
RLocationDatabase.java
public class RLocationDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "RGeoDbase";
public static final String TABLE_RG = "RGeoDbase";
public static final String PR_KEY = "ID";
public static final String R_ID = "RestaurantID";
public static final String R_Name = "Name";
public static final String R_Latitude = "Latitude";
public static final String R_Longitude = "Longitude";
public static final String [] COLUMNS = {PR_KEY,R_ID,R_Name,R_Latitude,R_Longitude};
public RLocationDatabase(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase data){
String CREATE_DB = "CREATE TABLE RGeoDbase ( " + "ID VARCHAR PRIMARY KEY, " + "Name TEXT, "
+ "Latitude DECIMAL(10,8), " + "Longitude DECIMAL(11,8) )";
data.execSQL(CREATE_DB);
}
@Override
public void onUpgrade(SQLiteDatabase data, int oldV, int newV){
data.execSQL("DROP TABLE IF EXISTS RGeoDbase");
this.onCreate(data);
}
public boolean insertRecord(String name, double latitude, double longitude){
SQLiteDatabase dbase = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(R_ID, r_id);
values.put(R_Name, name);
values.put(R_Latitude, latitude);
values.put(R_Longitude, longitude);
dbase.insert(TABLE_RG, null, values);
return true;
}
/*
public Cursor getData(String id){
SQLiteDatabase dbase = this.getWritableDatabase();
Cursor cursor = dbase.rawQuery("SELECT * FROM RGeoDbase WHERE Restaurant_ID="+id+"", null);
return cursor;
}*/
@SuppressWarnings("rawtypes")
public ArrayList<RDatabaseModel> getAllData(){
RDatabaseModel model = new RDatabaseModel();
ArrayList<RDatabaseModel> list = new ArrayList<RDatabaseModel>();
SQLiteDatabase dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery("SELECT * FROM RGeoDbase", null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
//model.setRestaurantID(cursor.getString(cursor.getColumnIndex(R_ID)));
model.setName(cursor.getString(cursor.getColumnIndex(R_Name)));
model.setLatitude(cursor.getDouble(cursor.getColumnIndex(R_Latitude)));
model.setLongitude(cursor.getDouble(cursor.getColumnIndex(R_Longitude)));
list.add(model);
cursor.moveToNext();
}
return list;
}
public void close(){
this.close();
}
}
RDatabaseModel.java
public class RDatabaseModel {
private String name;
private double latitude;
private double longitude;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
MainActivity.java
public class MainActivity extends Activity {
public static RLocationDatabase myDatabase;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDatabase = new RLocationDatabase(this);
buildDatabase();
setContentView(R.layout.activity_main);
ArrayList<RDatabaseModel> a = myDatabase.getAllData();
RDatabaseModel rd;
for(int i=0; i<a.size(); i++){
rd = a.get(i);
System.out.println(rd.getName());
}
}
public void buildDatabase(){
if(!myDatabase.insertRecord("Bijas Vegetarian", 22.28416252, 114.13412900)){
Log.v("1", "error inserting data #1");
}
myDatabase.insertRecord("Delifrance", 22.28328514, 114.13483800);
myDatabase.insertRecord("Ebeneezer's Kebabs and Pizzeria", 22.28362400, 114.13973600);
myDatabase.insertRecord("Bay View Restaurant", 22.26952800, 114.12982000);
myDatabase.insertRecord("C.C.Canteen", 22.28003600, 114.13058000);
myDatabase.insertRecord("Cafe 330", 22.28279200, 114.13907500);
myDatabase.insertRecord("Fong Shu Chen Amenities Centre Restaurant", 22.28383400, 114.13974600);
myDatabase.insertRecord("FRU:YO Factory", 22.28270600, 114.13874000);
myDatabase.insertRecord("Garden Patio", 22.26732000, 114.12837100);
myDatabase.insertRecord("Groove Cafe", 22.28312300, 114.13485300);
if(!myDatabase.insertRecord("Ho Tim Hall Restaurant", 22.28406700, 114.13283700)){
System.out.println("error inserting data #11");
}
myDatabase.insertRecord("Maxim's Food 2", 22.28309900, 114.13949100);
myDatabase.insertRecord("Pacific Coffee Company", 22.28363500, 114.13966600);
myDatabase.insertRecord("Starbucks Coffee (Main Campus)", 22.28312100, 114.13803300);
if(!myDatabase.insertRecord("Subway Sandwiches", 22.28264100, 114.13850600)){
Log.v("15","error inserting #15");
}
myDatabase.insertRecord("Union Restaurant", 22.28315100, 114.13590700);
}
}
答案 0 :(得分:1)
可能需要直接在while循环中创建数据库模型对象。
while(!cursor.isAfterLast()){
RDatabaseModel model = new RDatabaseModel(); // here
model.setName(cursor.getString(cursor.getColumnIndex(R_Name)));
model.setLatitude(cursor.getDouble(cursor.getColumnIndex(R_Latitude)));
model.setLongitude(cursor.getDouble(cursor.getColumnIndex(R_Longitude)));
list.add(model);
cursor.moveToNext();
}
答案 1 :(得分:0)
将ID设为id integer primary key autoincrement