dimanche 1 mars 2015

cursor is not displaying data in second activity

I have a list of items, the data are stored in the SQLite. The problem occurred when I wanted to get detailed view of the item, it doesn't display any data. This is activity that displays all rows from the database in a listview, works great.



public class List extends Activity {

public final static String ID_EXTRA="com.example.bazadanych._ID";

DBAdapter myDB;

String passedV=null;

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

passedV=getIntent().getStringExtra(MainActivity.ID_EXTRA);

openDB();

populateListViewDB ();

onclickcallback();

}

@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}

private void openDB() {
myDB = new DBAdapter(this);
myDB.open();

}

private void closeDB() {
myDB.close();

}

private void populateListViewDB() {
Cursor cursor = myDB.getAllRows();

startManagingCursor(cursor);

String[] fromFieldNames = new String[]
{DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_PHONE};
int [] toViewIDs = new int []
{R.id.list_item_name, R.id.list_item_country, R.id.list_item_region, R.id.list_icon_item};

SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.list_item,
cursor,
fromFieldNames,
toViewIDs
);


ListView myList = (ListView) findViewById(R.id.listViewDB);
myList.setAdapter(myCursorAdapter);

}
private void onclickcallback() {
ListView myList = (ListView) findViewById(R.id.listViewDB);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {

Intent intent = new Intent(List.this, Details.class);
intent.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(intent);
}
});
}
}


From the List.class after clicking on item from the list the user can move to details.class that is suppose to display details of the item. Unfortunately it doesn't display anything and doesn't give any error either.


I'm passing an Id from one activity to another, it moves from one to the other but later on doesn't display any data,



public class Details extends Activity {

DBAdapter myDB;

String passedV=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);

passedV=getIntent().getStringExtra(List.ID_EXTRA);
myDB = new DBAdapter(this);
}

private void populatedetailsViewDB() {
Cursor cursor = myDB.getRow(0);

startManagingCursor(cursor);


String[] fromFieldNames = new String[]
{DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_ADRESS, DBAdapter.KEY_PHONENUM, DBAdapter.KEY_PHONE};
int [] toViewIDs = new int []
{R.id.item_name, R.id.item_country, R.id.item_region, R.id.item_adress, R.id.item_phonenum, R.id.list_icon_item};

SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.item_layout,
cursor,
fromFieldNames,
);

ListView myList = (ListView) findViewById(R.id.detailsViewDB);
myList.setAdapter(myCursorAdapter);


}
}


That's getting a single row from the DBAdapter.



public class DBAdapter {

...

public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}

// Close the database connection.
public void close() {
myDBHelper.close();
}

...

public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}


Can anyone see what might be the problem?


how to add customer object to sqllite database

in my java code I have a customer object which has name, address, phone number, i have initialised these variables in the constructor, my question is, when a customer registers they put in their name, address, number etc via a guy frame, how do i put the information they've provided me into my database? Thankyou


jdbc sqlite returns no rows


public ArrayList<Booking> getAllBookings(){
ArrayList<Booking> list = new ArrayList<Booking>();
int rowCount = 0;
try {
stmt = connection.createStatement();
String sql = "SELECT * FROM Bookings";
ResultSet rows = stmt.executeQuery(sql);
rows.last();
System.out.println("Row Count "+rows.getRow());
rows.beforeFirst();
connection.commit();

while (rows.next()){
Booking b = new Booking();
otherStuff();
list.add(b);
rowCount++;
}
stmt.close();
rows.close();
} catch (Exception e) {

}

System.out.println("There were " +rowCount + " records.");
return list;
}


Why do I not get any rows returned? I am connecting using this:



public static Connection dbConnect(){
try {
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:D:/test/test_db.sqlite3");
System.out.println("Connection Succesful");
return connection;
} catch (Exception e) {
System.out.println(e);
return null;
} }


When running SQLite Studio my query returns the correct results, but here my output on console is:



Connection Succesful
(Row Count + rows.getRow() is not printing here for some reason)
There were 0 records.

Android onItemClick giving Incorrect id

I am trying to get the details for a clicked item in a ListView by querying the SQLite database. I am passing the long id value from onItemClick as the primary key. However, the long id does not match with the actual primary key in the Table. I found out the actual primary key by querying for the primary keys of all the rows in the Table.


I have the following questions: 1. Am I right in assuming that the long id parameter in onItemClick(AdapterView parent, View view, int position, long id) carries the primary key of the item in the database table?



  1. If not, how should I get the primary key?


I have appended my code below:



list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//Display Client Details
Intent displayclient = new Intent(view.getContext(), display_clientdetails.class);
Integer db_id = (int) id;

String TAG = "client_list";
Log.d(TAG,"Position is" +position +" ID is " +db_id);

displayclient.putExtra("client_id",db_id);
view.getContext().startActivity(displayclient);

}


});


the database in iphone app is not updated if i install update app from testflight or diawi

my app is on local database using sqlite. suppose the table in the database has 3 columns and i installed this app using either testflight or diawi. Now i add 4th column in the table and now if i installed this app through testflight or diawi then i cant see the updated database in the app (in iphone). I need to uninstall previuos version and fresh install the app from testflight or diawi, then i can see the updates which i changed in the app.


Does anyone facing same problem ???


Thanks in advance .......


Replace string in all columns of a SQLite database using one simple query

Is it possible to replace a given string in all columns of a SQLite database using one simple query?


Let's say I have a table with the four columns name, age, income and friends:



+------+-----+--------+---------+
| name | age | income | friends |
+------+-----+--------+---------+
| John | 20 | 10 | 0 |
+------+-----+--------+---------+
| Jane | 25 | 20 | 0 |
+------+-----+--------+---------+
| June | 20 | 40 | 20 |
+------+-----+--------+---------+


And I want to replace all instances of "20" with "40" in the whole table using one simple query. Something like:



UPDATE table SET ALL-COLUMNS = 40 WHERE ALL-COLUMNS = 20


Is this possible to achieve using SQLite? Or do I have to create one query for each column?



UPDATE table SET age = 40 WHERE age = 20
UPDATE table SET income = 40 WHERE income = 20
UPDATE table SET friends = 40 WHERE friends = 20

how to merge two sqlite3 databese with same schema. if the entries found to be same then merge with the entry with latest timestamp

As I am new to the sqlite3 database, I want to merge two sqlite3 databases with same schema with uniqe keyword. But while merging if two entries from two databases are found to be same, then I want to merge the entry which has the latest time stamp.


For example I have two databases : 1. student_old.db 2. student_new.db


Schema for both DB is :


CREATE TABLE student (


'idx' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,


'name' CHAR(32) NOT NULL,


'age' INTEGER NOT NULL default 0,


'lastUpdateDate' timestamp DATE DEFAULT (datetime('now','localtime')),


UNIQUE (name)


);


Entries for two dbs are :


student_old.db


sqlite3 student_old.db


SQLite version 3.6.20


Enter ".help" for instructions


Enter SQL statements terminated with a ";"


sqlite> select * from student;


1|abcd|20|2015-03-01 00:32:29


2|efgh|22|2015-03-01 00:32:29


3|ijkl|24|2015-03-01 00:32:29


sqlite>


student_new.db


sqlite3 student_new.db


sqlite> select * from student;


SQLite version 3.6.20


Enter ".help" for instructions


Enter SQL statements terminated with a ";"


1|abcd|28|2015-03-01 00:32:34


2|mnop|26|2015-03-01 00:32:34


3|qrst|27|2015-03-01 00:32:34


sqlite>


I am merging the student_new.db to student_old.db with the below query


sqlite3 student_old.db <<"EOF"


attach './student_new.db' as toMerge;


BEGIN;


insert or ignore into student (name, age,lastUpdateDate)


select name, age, lastUpdateDate from toMerge.student;


COMMIT;


.q


So after the merging I will get


sqlite3 student_old.db


SQLite version 3.6.20


Enter ".help" for instructions


Enter SQL statements terminated with a ";"


sqlite> select * from student;


1|abcd|20|2015-03-01 00:32:29 ------>>>> I got the time stamp of student_old.db


2|efgh|22|2015-03-01 00:32:29


3|ijkl|24|2015-03-01 00:32:29


5|mnop|26|2015-03-01 00:32:34


6|qrst|27|2015-03-01 00:32:34


sqlite>


But I want to merge the entries of same record but with the latest time stamp. Something like this :


sqlite> select * from student;


1|abcd|20|2015-03-01 00:32:34 ------>>>> I want entry of latest time stamp for same record.


2|efgh|22|2015-03-01 00:32:29


3|ijkl|24|2015-03-01 00:32:29


5|mnop|26|2015-03-01 00:32:34


6|qrst|27|2015-03-01 00:32:34


sqlite>


Please let me know the query to merge the latest timestamp if I found the same records in two databases.


Your help would be highly appreciated.