Strange problem of updating database

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Strange problem of updating database

Postby cefobid2008 » Thu Jan 14, 2010 11:21 am

I am coding something on database, but encounter a strange problem when I want to update a specified row in table. The code is like:

private void addNewQuake(Quake _quake) {

ContentResolver cr = getContentResolver();

Cursor c = cr.query(...sth...);
int dbCount = c.getCount();

if (dbCount == 0) {
ContentValues values = new ContentValues();
values.put(...key, value...);
.....//some other values.put()
cr.insert(EarthquakeProvider.CONTENT_URI, values);

}else if (dbCount == 1) {
ContentValues values = new ContentValues();
values.put(...key, value...);
.....//some other values.put()
c.moveToFirst(); //
int id = c.getInt(0);
Uri rowUri = Uri.withAppendedPath(baseuri, id+"");
cr.update(rowUri, values, null, null);
}
}

The problem is values.put(...key, value...) in case of dbCount == 1 is always causing the process dead. In case of dbCount == 0, no problem at all.

Later, I tried to move the corresponding value initialization out of scope of dbCount == 1, and place it before database querying, such like following code, everything is ok:

private void addNewQuake(Quake _quake) {

ContentValues values_x = new ContentValues();
values_x.put(...key, value...);
..... //some other values_x.put()

ContentResolver cr = getContentResolver();

Cursor c = cr.query(...sth...);
int dbCount = c.getCount();

if (dbCount == 0) {
ContentValues values = new ContentValues();
values.put(...key, value...);
.....//some other values.put()
cr.insert(content_uri, values);

}else if (dbCount == 1) {

c.moveToFirst(); //move cursor on the result
int id = c.getInt(0);
Uri rowUri = Uri.withAppendedPath(content_uri, id+"");
cr.update(rowUri, values_x, null, null);
}
}

So, it seems like if any result row would be returned, I can not put value in a created ContentValue. However, I can do that before querying. Who knows why???
cefobid2008
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 06, 2010 11:18 am

Top

Postby jwei512 » Fri Jan 15, 2010 12:30 pm

What exactly is the error? Is it an SQL error?
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby cefobid2008 » Mon Jan 18, 2010 2:44 am

jwei512 wrote:What exactly is the error? Is it an SQL error?


The error is the main process is terminated by signal 11. However,

I reboot the pc, the problem disappeared.

No idea at all..
cefobid2008
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 06, 2010 11:18 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests