Reputation: 129
I'm trying to add multiple items to database. However, during read operation, I getting always the last item. I got many item as I added. However, they have same content of last item added.
Example:
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]
What I read is:
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],
....
This is how I add them to database
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));
}
i just use getall fucntion to get them.
and this is my helper class.
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());
}
}
Upvotes: 1
Views: 46
Reputation: 18677
I think the problem is your Yen class
static private int id;
static private Double Lng;
static private Double Lat;
static private String Name
I think the problem are the Yen variable members... they are static. So, all objects are sharing same memory position (and consequently, show only the last value saved)
Remove static modifier of those Yen class members
Upvotes: 3
Reputation: 2117
Try with this:
if (cursor.moveToFirst()) {
do {
// Add book to books
yerler.add(new Yer(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getDouble(2),
cursor.getDouble(3)));
} while (cursor.moveToNext());
}
Upvotes: 1