I am working on a project where I need to store multiple sessions. When a user logs in the system, his username is stored in the app. So the next time app is run, the username is displayed on the home screen. He can either click on that or add new account, which then would get populated in the list too. I read the notepad tutorial on android developers website to learn how to use SQLite in android, but I couldn't make it work for my case.
I am storing the username in a string variable 'value' and I want this 'value' to be added in the database and displayed as a list on the screen.
Can anyone please help me on this? IF you could give some links of tutorials that do exactly what I am trying to do, that would help too. Thanks!
declare this first
private SQLiteDatabase db;
to create table
this.db=this.openOrCreateDatabase("filestore", MODE_PRIVATE, null);
this.db.execSQL("CREATE TABLE IF NOT EXISTS usernames(name text)");
insert
String username;
db.execSQL("insert into usernames values ('"+username+"')");
retrieve
this.db = this.openOrCreateDatabase("filestore", MODE_PRIVATE, null);
db_results = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT name FROM usernames", null);
while (cursor.moveToNext()) {
db_results.add(String.valueOf(cursor.getString(cursor
.getColumnIndex("name"))));
// db_results.add(String.valueOf(cursor.getString(cursor.getColumnIndex("name"))));
}
cursor.close();
db.close();
This tutorial from markana helped me to grab the basics of the sqlite and android. Hope it'll help you too.
another example, and one more too
Related
I am on Android and this question is of Sqlite :
I have a table (USERLOGIN) which holds the user's credentials (username and password) in it. I have another table ($USER$_PROJS) which holds information of projects for a particular user. Users can create and add projects in there.
[ $USER$ will be a variable which comes from the column username in USERLOGIN table. So its basically a dynamic table creation. ]
Both of these tables are in the same database USER.db.
I have only one LoginDatabseAdapter class and DatabseHelper class which manages both of these.
Now initially when user logs-in, the database is behaving properly. But when inside the user profile when a user tries to create/add project its not inserting the values into $USER$_PROJS table.
I think i need to use the USE TABLE (once the user successfully logs-in his profile) like statement but its giving an error when i try to use it.
I have searched almost all the resources on net but was unable to find the solution !!
Any help would be appreciated !!
CODE RESPONSIBLE FOR CREATING TABLE :
public void createprojtable(String u){
db.execSQL(
"create table if not exists "+u+"_PROJS(ID integer primary key autoincrement,
PROJ_NAME text,DATE text); ");
}
public void insertProjEntry(String u,String projName,
String projdate) {
//db.execSQL("use table "+u+"_PROJS;");
ContentValues newValues = new ContentValues();
newValues.put("PROJ", projName);
newValues.put("DATE", projdate);
db.insert(u+"_PROJS", null, newValues);
}
Have a look at the Cursors for Sqlite in Android. You get Cursors specific to a table. Using the Cursors you can read data from a specific table as such. While updating the data also you can specify table name as one of the parameters. So you can pretty much get what you are looking for using existing APIs.
Have a look at following links :
Android SQLite Database and ContentProvider - Tutorial
android.database.sqlite
I'm trying to query on a table dormpolicy
String operatorName = "46001";
String selection = "(plmn = '"+operatorName+"')";
URI CONTENT_URI_DORMPOLICY = Uri.parse("content://nwkinfo/nwkinfo/dormpolicy");
cursor = phone.getContext.getContentResolver().query(CONTENT_URI_DORMPOLICY, null, selection, null, null);
I get error log:
SQLiteQuery: exception: no such table: dormpolicy;
query: SELECT * FROM dormpolicy WHERE ((plmn = '46001'))
This issue happen not always in my phone.
It seems that sometimes,when I access the table before the table was created.
Is there any way that before I query the table, I check if the table exist?
And how?
My answer is twofold:
To strictly answer the question, I will redirect you to How does one check if a table exists in an Android SQLite database?
But rather than manually checking for tables' existence, I suggest you use the SQLiteOpenHelper class. This will help you manage your tables in a structured way. That includes handling database upgrades. Have a look at this for a quick start: http://developer.android.com/guide/topics/data/data-storage.html#db
I am trying to update a single column in a SQLite database table in my Android project. What I want to do is set the value of "done" column to 1, when the vehicle number is same. I preferred to go with sql queries other than using myDB.update. So I used this query,
update 'details' set done = 1 where vehicle=="*****"
But the problem is, this query is working perfectly in the sqlite databse browser, not in android simulator. This is completely not working at the android simulator. Hope ur advice.
Thanks in advance.
//Declaration
SQLiteDatabase dbx;
ContentValues cv1;
EventDataSQLHelper eventsData;//object of class in which table is created
//on create
eventsData = new EventDataSQLHelper(this);
dbx= eventsData.getReadableDatabase();
cv1 = new ContentValues();
cv1.put("done",1);
//update query
dbx.update("details", cv1, "vehicle=" ? , null);
Hi
I have created a database for my application and i have added items to the database using methods from the database class. I am encountering a problem do when i try to execute a sql query in the other class(app.java), i need to reference a database and thats where im having the problem!
this is the sql query im trying to execute(in database.java)
public void getData(SQLiteDatabase db, String data){
String sql =
"SELECT permissions FROM genres WHERE name = "+data+";";
db.execSQL(sql);
}
and this is how i am calling it(in app.java)
appData.getData(db, chosenGenre);
I just dont no what to put for the "db" part in appData.getPermissions(db, chosenGenre);
Does anyone know how to do this?
Thanks
You would typically use a SQLiteOpenHelper to create the database files (if necessary) and obtain a SQLiteDatabase object which is used to access the actual database (files on disk read by SQLite).
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
Here's a nice tutorial
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/2/
You don't show the code where you define the database manager 'db'! If you are using a database created outside the android device, you might get some help from this link:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Also, I have found that it's best to frame all table names and supplied values in single quotes a la -
String sql = "SELECT permissions FROM 'genres' WHERE name = '" + data + "'";
Note that the semicolon is not required.
I am new to Android App development and was hoping if someone could help me with the sqlite query to search an item.
I want to take a string from the Edittext box and then use that string to search my sqlite database to display the entries back on the android screen. How can I do this?
For example if I wanted to find-
select * from table_name where name= str1; (here str1 is the string from edittext box).
How do I write the java code for this so that it displays all the information regarding str1 from the database to the android screen?
I appreciate your help
:-) The general way to go about it is to use something called Cursor. In the following code, db is the database.
Cursor cur = db.rawQuery("SELECT * FROM "+ table_name + ";");
You'll have to modify it to use your query instead. The next step is to get the values from the Cursor. textview1 and so on are TextView's in your UI. I'll assume you know how to do this, but if you need help with this please let me know. nameOfColumn is the name of the columns in your database (I think that's name in your case)
if(cur.moveToFirst()) {
if(cur != null){
//Display the text in TextView's
textview1.setText(cur.getString(cur.getColumnIndex("nameOfColumn"));
textview2.setText(cur.getString(cur.getColumnIndex("nameOfColumn2"));
}
}
Hope this helps! :-)