简体   繁体   English

SQLite数据库不存储数据-Android

[英]SQLite database not storing data - Android

I have implemented a static sort of SQLite Database for my app. 我为我的应用程序实现了一种静态的SQLite数据库。 The app can only read from the database once it is built. 该应用一旦构建便只能从数据库中读取。 There are no methods for update or delete. 没有更新或删除的方法。 I get an exception in LogCat that RestaurantID is not unique. 我在LogCat中得到一个异常,即RestaurantID不是唯一的。 Here is my implementation: 这是我的实现:

RLocationDatabase.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 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 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: 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)
ArrayList a = myDatabase.getAllData();
for(int i=0; i<a.size(); i++){
    System.out.println(a.indexOf(i));

indexOf() retrieves the index of the specified object. indexOf()获取指定对象的索引。 The list does not have an object "0" for example and index -1 is returned. 例如,列表中没有对象“ 0”,并且返回索引-1。

If you intended to print the object itself, replace 如果要打印对象本身,请更换

a.indexOf(i)

with

a.get(i)

To get rid of the "not unique", move the buildDatabase() inserts to your database helper onCreate() so they are run only once when the database is created. 要摆脱“非唯一”问题,请将buildDatabase()插入项移动到数据库助手onCreate()以便在创建数据库时仅将它们运行一次。

In your table "RestaurantID" is PRIMARY KEY.There is no need to define value for primary key. 在您的表中,“ RestaurantID”是PRIMARY KEY。无需定义主键的值。

Can you try it like this too : 你也可以这样尝试吗:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM