我正在尝试将多个项目添加到数据库中。但是,在读取操作期间,我总是得到最后一项。我添加了很多项目。但是,它们具有与最后添加的项目相同的内容。
例子:
06-03 18:30:19.343 16360-16360/com.example.wince.myapplication D/add: Yer [id=112, name=syndey, lat=-34.0, lng=151.0]
06-03 18:30:19.393 16360-16360/com.example.wince.myapplication D/add: Yer [id=112, name=syndey, lat=-34.0, lng=151.0]
06-03 18:30:19.433 16360-16360/com.example.wince.myapplication D/dalvikvm: GC_FOR_ALLOC freed 1776K, 49% free 10398K/20120K, paused 35ms, total 35ms
06-03 18:30:19.443 16360-16360/com.example.wince.myapplication D/add: Yer [id=112, name=syndey, lat=-34.0, lng=151.0]
06-03 18:30:19.483 16360-16360/com.example.wince.myapplication D/add: Yer [id=112, name=syndey, lat=-34.0, lng=151.0]
06-03 18:30:19.504 16360-16360/com.example.wince.myapplication D/add: Yer [id=112, name=istanbul, lat=-34.0, lng=151.0]
06-03 18:30:19.534 16360-16360/com.example.wince.myapplication D/add: Yer [id=112, name=vggg, lat=-34.0, lng=151.0]
06-03 18:30:19.554 16360-16360/com.example.wince.myapplication D/add: Yer [id=112, name=ankara, lat=-34.0, lng=151.0]
06-03 18:30:19.584 16360-16360/com.example.wince.myapplication D/add: Yer [id=112, name=bagdat, lat=-34.0, lng=151.0]
我读到的是:
D/getAllBooks(): [Yer [id=120, name=bagdat, lat=-34.0, lng=151.0],
Yer [id=120, name=bagdat, lat=-34.0, lng=151.0],
Yer [id=120, name=bagdat, lat=-34.0, lng=151.0],
Yer [id=120, name=bagdat, lat=-34.0, lng=151.0],
Yer [id=120, name=bagdat, lat=-34.0, lng=151.0],
Yer [id=120, name=bagdat, lat=-34.0, lng=151.0],
Yer [id=120, name=bagdat, lat=-34.0, lng=151.0],
Yer [id=120, name=bagdat, lat=-34.0, lng=151.0],
....
这是我将它们添加到数据库的方式
public void onClick(DialogInterface dialog2, int which) {
m_Text = input.getText().toString();
LatLng sydney = new LatLng(-34, 151);
VT.add(new Yer("syndey", sydney.latitude, sydney.longitude));
VT.add(new Yer("syndey", sydney.latitude, sydney.longitude));
VT.add(new Yer("syndey", sydney.latitude, sydney.longitude));
VT.add(new Yer("syndey", sydney.latitude, sydney.longitude));
VT.add(new Yer("istanbul", sydney.latitude, sydney.longitude));
VT.add(new Yer(m_Text, sydney.latitude, sydney.longitude));
VT.add(new Yer("ankara", sydney.latitude, sydney.longitude));
VT.add(new Yer("bagdat", sydney.latitude, sydney.longitude));
}
我只是使用 getall 函数来获取它们。
这是我的助手类。
package com.example.wince.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.LinkedList;
import java.util.List;
/**
* Created by Wince on 2.06.2016.
*/
public class VeriTabani extends SQLiteOpenHelper {
// table name
private static final String TABLE = "Kayitlar";
// Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "isim";
private static final String KEY_LAT = "latitude";
private static final String KEY_LNG = "longitude";
private static final String[] COLUMNS = {KEY_ID,KEY_NAME,KEY_LAT,KEY_LNG};
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "KayitDB";
public VeriTabani(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create table
String CREATE_TABLE = "CREATE TABLE Kayitlar ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"isim TEXT, " +
"latitude REAL,"+
"longitude REAL )";
// create kayıtlar table
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older books table if existed
db.execSQL("DROP TABLE IF EXISTS ");
// create fresh books table
this.onCreate(db);
}
public void add(Yer veri){
//for logging
Log.d("add", veri.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_NAME, Yer.getName()); // get title
values.put(KEY_LAT,Yer.getLat() ); // get author
values.put(KEY_LNG,Yer.getLng());
// 3. insert
db.insert(TABLE, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
public Yer getYer(int id){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String[] { String.valueOf(id) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
// 4. build object
Yer yer = new Yer();
yer.setId(Integer.parseInt(cursor.getString(0)));
yer.setName(cursor.getString(1));
yer.setLat(cursor.getDouble(2));
yer.setLng(cursor.getDouble(3));
Log.d("getBook("+id+")", yer.toString());
// 5. return
return yer;
}
// Get All
public List<Yer> getAllYer() {
List<Yer> yerler = new LinkedList<Yer>();
// 1. build the query
String query = "SELECT * FROM " + TABLE;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build book and add it to list
Yer yer = null;
if (cursor.moveToFirst()) {
do {
yer = new Yer();
yer.setId(Integer.parseInt(cursor.getString(0)));
yer.setName(cursor.getString(1));
yer.setLat(cursor.getDouble(2));
yer.setLng(cursor.getDouble(3));
// Add book to books
yerler.add(yer);
} while (cursor.moveToNext());
}
Log.d("getAllBooks()", yerler.toString());
// return books
return yerler;
}
// Updating single
public int updateBook(Yer yer) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("Name", yer.getName()); // get title
values.put("Lat", yer.getLat()); // get author
values.put("Lng",yer.getLng());
// 3. updating row
int i = db.update(TABLE, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(yer.getId()) }); //selection args
// 4. close
db.close();
return i;
}
// Deleting single
public void deleteBook(Yer yer) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. delete
db.delete(TABLE,
KEY_ID+" = ?",
new String[] { String.valueOf(yer.getId()) });
// 3. close
db.close();
Log.d("deleteBook", yer.toString());
}
}
最佳答案
我认为问题出在你的 Yen 类上
static private int id;
static private Double Lng;
static private Double Lat;
static private String Name
我认为问题出在 Yen 变量成员上……它们是静态的。因此,所有对象都共享相同的内存位置(因此,只显示最后保存的值)
去掉那些Yen类成员的static修饰符
关于android - 在android中添加到SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37618449/