I have the following method in my SQL class:
public void deleteEntry(int idnum) {
dbSQL.delete(DATABASE_TABLE, KEY_ROWID + " = 0", null);
}
I usually give it an idnum but for testing purposes I've just put a 0 there. Syntax wise, is there any reason why this wouldn't work? I've tried everthing.
Assuming that KEY_ROWID is an autoincrementing primary key, the first record you enter will have value 1, not 0.
That being said, the reason why your delete method isn't working is most likely because there is no record with value 0.
Edit #1:
If I were you, I would uninstall and re-install your app and start from scratch. Either that, or use adb to analyze the contents of your SQLite database (using the emulator, if you don't have a rooted device). You can find the database file on the disk at /data/data/[package name]/databases/database_name.db (or something like that).
The best way to figure out why things aren't working at this point is to analyze the contents of the database. Otherwise you will just be guessing and checking until something appears to work... and even then you probably won't know for sure if your implementation is 100% correct.
Edit #2:
As an alternative to analyzing the database file using the emulator and/or your rooted device, you could perform a query for ALL data in the database, and then use the dumpCursor method provided in the DatabaseUtils class to print the contents of the Cursor to System.out.
You should use this version,
public void deleteEntry(int idnum) {
dbSQL.delete(DATABASE_TABLE, KEY_ROWID + " = ?", new String[] {"0"});
}
With 0 it will probably not work because if the column is INTEGER and AUTOINCREMENT it will start counting at a value greater then 0 so there isn't a row with the KEY_ROWID 0 to delete.
Change it to,
public void deleteEntry(int idnum) {
dbSQL.delete(DATABASE_TABLE, KEY_ROWID+"=?", new Integer[]{idnum});
}
if your KEY_ROWID column is string than you have to use
public void deleteEntry(int idnum) {
dbSQL.delete(DATABASE_TABLE, KEY_ROWID + " = '0'", null);
}
Thanks for the help everyone, but I had some luck by just using a method I had built that took the id and returned the name associated with that entry and it successfully deletes corresponding items now.
Related
I am trying to query my database by using a Java function with another attribute defined in the database. The statement generates no error. However, the output is wrong. The result of the output is null but from my checking it is not null. Please can anyone tell me what I need to do? How can I use JAVA functions in SQl statements?
expired_rows = dbMngr.runQuery(String.format("SELECT ID FROM Student WHERE 'System.currentTimeMillis()' - ArrivalTime > (%s) ", 5000));}
if (expired_rows == null) {
System.out.println("The number of expired row is " + expired_rows);
}
if (expired_rows != null) {
System.out.println("The number of expired row is " + expired_rows.length );
}
You can't use the java function in the way you are trying to. You are just passing the String "'System.currentTimeMillis()'" to the dbms - you want to pass in the result of evaluating that function
Would
"SELECT ID FROM Student WHERE %l - ArrivalTime > (%s) ",System.currentTimeMillis(), 5000));}"
work? ( I'm unsure of the syntax for String.format and don;t have acces to the docs, but hopefully you get the picture ... )
A better solution, but a little further away from the work you have already done, would be to use a PreparedStatement as suggested by GriffeyDog.
Also, if no error is being generated, then it seems a quite likely that your dbMngr class is swallowing the exceptions without reporting them.
JdbcTemplate.update() returns number of rows affected - so you not only know that delete/update was sucessfull, you also know how many rows were deleted/updated.
What will be the return value if I try to insert a row.
Is it possible to get return value as "0"??
private static final String insertSql =
"INSERT INTO employee (" +
" name, " +
" surname, " +
" title, " +
" created) " +
"VALUES (John, Smith, Software developer, new Date())";
int row = template.update(insertSql, params, types);
Yes, in theory you should get 0 or 1, but if no row was inserted, it would be due to an error, so a DataAccessException would be thrown, which is how you would know that something went wrong and no row was created.
jdbctemplate.update will return in integer format as we know.
in order to find 0 or 1 just do this below simple code
int i=jdbctemplate.update(.your db call..);
it will return 1 for success and 0 for failure case so, according to it you can process your logic.
To answer your question, yes, as #Turophile pointed out, you will get a 0 or 1 in response as you can tell from the method signature (which returns an int).
In response to #Turophile comment (sorry I can't add a comment :/ ), to avoid this and avoid partial data being saved to the database, use Springs Transaction Management (tx). This would allow you to rollback transactions based on specific exceptions or all exceptions from the Exception class. Mind you, by default, #Rollback rolls back transactions for runtime, unchecked exceptions only.
#Transactional(rollbackFor = Exception.class)
public Employee updateEmployee(Employee pEmployee) { ... }
The #Transactional can also be added to the class as well, but I would read up more on it if you are interested in implementing this feature. There is a lot of good documentation on tx.
As a side note, I don't recommend lying to the caller of your method. If you call a method that says "update", run a query that'll update the record, if you want to insert/create a new record, create or call a method that "inserts" a new record into your table -- inserting a duplicate record wouldn't work anyways if the primary key(s) are unique.
The Spring jdbcTemplate class doesn't provide an 'insert' method, only query and update.
I've watched countless tutorials and examples about SQLite on Android but I just don't get some things. could someone please explain some things to me?
This the code that I came up with until now (SQLite helper):
public class SQLiteHelper extends SQLiteOpenHelper {
public static String DATABASE_NAME = "urnik";
public static int DATABASE_VERSION = 1;
public SQLiteHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
//Tabela prva ura
public static String IME_TABELE = "prva_ura";
public static String ID_PREDMETA = "id_predmeta";
public static String NAZIV_PREDMETA = "naziv_predmeta";
String prva_query =
"CREATE TABLE " + IME_TABELE + " ("
+ ID_PREDMETA + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAZIV_PREDMETA + " TEXT" + ");";
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(prva_query);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
So as far as I know, this should create a new database and one table called "prva_ura". But I have a lot of questions about this.
I only created this class, I haven't attached it to MainActivity or anything, will the database still be created when app is ran for the first time or do I have to do something else?
I'm assuming the database is created when the app is launched for the first time after creating database. Can I find the database file anywhere to check if everything is as it's supposed to be?
I'd appreciate some tips in general, thank you!
The database is first created when either getReadableDatabase() or getWriteableDatabase() are called. These methods will create the db if it doesn't exist or open an existing one.
So for you to create it, just query it. Write some CRUD methods and the first one that is called will create your db.
To upgrade your database, simply increment the database version and it will auto update.
From dev page getWriteableDatabase()
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase()
Create and/or open a database that will be used for reading and
writing. The first time this is called, the database will be opened
and onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int)
and/or onOpen(SQLiteDatabase) will be called.
Once opened successfully, the database is cached, so you can call this
method every time you need to write to the database. (Make sure to
call close() when you no longer need the database.) Errors such as bad
permissions or a full disk may cause this method to fail, but future
attempts may succeed if the problem is fixed.
All you have to do is create an instance of your class, and the database plus all tables get created.
Once the database is created, then next time your app runs, the onCreate() doesn't get called again.
As for checking that everything has been created properly, you can use the sqlite command line client
http://www.sqlite.org/sqlite.html
Details of the database location on your device are here Location of sqlite database on the device
Yes and yes.
I would advise you to look into motodev as this give you a nice database view in eclipse which is much easier to work with. It also handles generating ContentProviders for you so you do not have to write code as you have done above, simply define your database using the database view and generate the ContentProviders.
Saved me a lot of hours.
You need to call the onCreate method first probably in your main activity or any other activity to create your activity when yor app runs.
Change your onUpgrade code to this
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS prva_ura");
onCreate(db);
}
Yes there is a place where you can find your database if you switch from java to DDMS
I have a strange problem to update a table in my database...forgive me if I can not explain well but I'm a bit confused...
The problem is this:
I created a table with values, I read this values in my listview..everything works for now..insert and delete values works without problem..now created a loop in a service why do I need to make a comparison between a value and a string of my database and when this comparison is true, I need to change a value in my table..
The real problem is this: my db.update works only if not use ... never, the command db.delete... if use it, the db.update not work anymore ..and to make it work again, i need to make a new AVD.
how is it possible?
my db.delete and id is this:
item.getMenuInfo();
id = getListAdapter().getItemId(info.position);
public void deleteReg(SQLiteDatabase db ,long id)
{
db.delete(TabRegistry.TABLE_NAME, TabRegistry._ID + "=" + id, null);
}
on activity:
databaseHelper.deleteReg(db, id);
my db.update is this: (positions is a value of getPositions(),for locate a positions with a cursor(always works, even when fails db.update))
public void updateReg(SQLiteDatabase db,int positions, String stat)
{
ContentValues v = new ContentValues();
v.put(TabRegistry.STATUS, stat);
db.update(TabRegistry.TABLE_NAME, v, TabRegistry._ID + " = " + positions, null);
}
on service:
databaseHelper.updateReg(db, positions, "SUCCESS");
if you need more code, tell me what I add now..thanks in advance
The SQLite api you are using is based off of CRUD operations (you should read this).
You are DELETE-ing the record from the database, therefore there is nothing to UPDATE when you attempt to update it. If you want to create a new record, or recreate the one you deleted then you would perform an INSERT instead of an UPDATE.
EDIT:
It also appears you are passing in position number to the update and delete. I assume that you are also using this value to place the record in your table? Is it possible that when you delete the record from the table and the database, that the other records now have an invalid position because they haven't been updated also? It's just a shot in the dark, figured I might as well ask.
Using the GeoTools WFS-T plugin, I have created a new row, and after a commit, I have a FeatureId whos .getId() returns an ugly string that looks something like this:
newmy_database:my_table.9223372036854775807
Aside from the fact that the word "new" at the beginning of "my_database" is a surprise, the number in no way reflects the primary key of the new row (which in this case is "23"). Fair enough, I thought this may be some internal numbering system. However, now I want a foreign key in another table to get the primary key of the new row in this one, and I'm not sure how to get the value from this FID. Some places suggest that you can use an FID in a query like this:
Filter filter = filterFactory.id(Collections.singleton(fid));
Query query = new Query(tableName, filter);
SimpleFeatureCollection features = simpleFeatureSource.getFeatures(query);
But this fails at parsing the FID, at the underscore of all places! That underscore was there when the row was created (I had to pass "my_database:my_table" as the table to add the row to).
I'm sure that either there is something wrong with the id, or I'm using it incorrectly somehow. Can anyone shed any light?
It appears as if a couple things are going wrong - and perhaps a bug report is needed.
The FeatureId with "new" at the beginning is a temporary id; that should be replaced with the real result once commit has been called.
There are a number of way to be aware of this:
1) You can listen for a BatchFeatureEvent; this offers the information on "temp id" -> "wfs id"
2) Internally this information is parsed from the Transaction Result returned from your WFS. The result is saved in the WFSTransactionState for you to access. This was before BatchFeatureEvent was invented.
Transaction transaction = new transaction("insert");
try {
SimpleFeatureStore featureStore =
(SimpleFeatureStore) wfs.getFeatureSource( typeName );
featureStore.setTransaction( transaction );
featureStore.addFeatures( DataUtilities.collection( feature ) );
transaction.commit();
// get the final feature id
WFSTransactionState wfsts = (WFSTransactionState) transaction.getState(wfs);
// In this example there is only one fid. Get it.
String result = wfsts.getFids( typeName )[0];
}
finally {
transaction.close();
}
I have updated the documentation with the above example:
http://docs.geotools.org/latest/userguide/library/data/wfs.html