SQLite数据库不存储数据 - Android

时间:2014-06-18 08:34:23

标签: android sqlite

我为我的应用程序实现了一个静态的SQLite数据库。应用程序只能在构建后从数据库中读取。没有更新或删除方法。我在LogCat中得到一个例外,那就是RestaurantID不是唯一的。这是我的实施:

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 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 = {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 ( " + "RestaurantID VARCHAR(4) 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 void insertRecord(String id, String name, double latitude, double longitude){
    SQLiteDatabase dbase = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(R_ID, id);
    values.put(R_Name, name);
    values.put(R_Latitude, latitude);
    values.put(R_Longitude, longitude);

    dbase.insert(TABLE_RG, null, values);
}

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 getAllData(){
    ArrayList list = new ArrayList();
    SQLiteDatabase dbase = this.getReadableDatabase();

    Cursor cursor = dbase.rawQuery("SELECT * FROM RGeoDbase", null);
    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        list.add(cursor.getString(cursor.getColumnIndex(R_Name)));
        cursor.moveToNext();
    }
    return list;
}

MainActivity.java

public class MainActivity extends Activity {

private RLocationDatabase myDatabase;
private SQLiteDatabase data;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

myDatabase = new RLocationDatabase(this);
buildDatabase();

ArrayList a = myDatabase.getAllData();
    for(int i=0; i<a.size(); i++){
        System.out.println(a.indexOf(i));
}
}

public void buildDatabase(){
        myDatabase.insertRecord("0001", "ABC Fastfoods", 22.28416252, 114.13412900);
    }
    }

logcat的:

06-18 16:37:43.359: E/SQLiteDatabase(25373): Error inserting Name=ABC Fastfoods Latitude=22.28416252 Longitude=114.134129 RestaurantID=0001
06-18 16:37:43.359: E/SQLiteDatabase(25373): android.database.sqlite.SQLiteConstraintException: column RestaurantID is not unique (code 19)

2 个答案:

答案 0 :(得分:1)

ArrayList a = myDatabase.getAllData();
for(int i=0; i<a.size(); i++){
    System.out.println(a.indexOf(i));

indexOf()检索指定对象的索引。例如,列表没有对象“0”,并返回索引-1。

如果您打算打印对象本身,请替换

a.indexOf(i)

a.get(i)

要删除“not unique”,请将buildDatabase()插入移动到数据库助手onCreate(),这样它们只能在创建数据库时运行一次。

答案 1 :(得分:1)

在您的表中,“RestaurantID”是PRIMARY KEY。无需为主键定义值。

你能这样试试吗:

myDatabase.insertRecord("ABC Fastfoods", 22.28416252, 114.13412900);