column is no created getting exception android - android

Well this is my DatabaseHelper class
public class DatabaseHelper extends SQLiteOpenHelper {
static final String dbName="demoDB";
public DatabaseHelper(Context context) {
super(context, dbName, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS players (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"playerName TEXT)";
db.execSQL(sql);
ContentValues values = new ContentValues();
values.put("playerName", "John");
db.insert("players", "playerName", values);
values.put("playerName", "George");
db.insert("players", "playerName", values);
values.put("firstName", "Marie");
db.insert("players", "playerName", values);
System.out.println("Hello");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS players");
onCreate(db);
}
}
And in my main activity this is what I do to get its data.
if (cursor!= null) {
if (cursor.moveToFirst()) {
do {
System.out.println(cursor.getString(cursor.getColumnIndexOrThrow("playerName")));
} while (cursor.moveToNext());
}
}
Why the column is not created?
and I am getting this exception.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.Stats}: java.lang.IllegalArgumentException: column 'playerName' does not exist

Are you selecting anything before you interact with the cursor? Your cursor should be the result from a "Select..." SQL call, but I don't see one in your DatabaseHelper or anything in your MainActivity to indicate you are doing any sort of selection.
Also, when you are inserting values into your table , I believe the second parameter should be null [db.insert("players", null, values)]. It's intended for the nullColumnHack, which you use if you aren't entering any data into row.
nullColumnHack optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
Edit
Well, so far you've created a table and inserted some records. The next step would be to create basic CRUD operations (Create, Retrieve, Update, Delete), but for your case, you need to start with Retrieve.
Retrieve basically means you are reading/retrieving data from your database, so you can use it in your application. When you read the data from a DB, it gets stored in a Cursor. So, you need to:
Create a method in your database helper to read data from your database
From your main activity, call the method you created in step #1. Result is stored in a cursor.
In your main method, extract the data from the Cursor so you can use it. (Looks like you've already attempted this in your main activity).
Here is a link to a great tutorial on setting up a databse and how to create CRUD operations for your database. For now, scroll down and read up on the Retrieve operation.
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
I would read the entire tutorial if I were you. There are lots of other helpful tips and suggestions that will be well worth the investment now. For example, creating constants in your DatabaseHelper class for the table name, database name, column name is a really good idea because sooner or later you're going to make a typo if you keep typing in all the column and table names manually. If you use variables, you'll get a compile error rather than an exception at runtime.

Related

Obtaining table names for sqlite SQL join in android

I have a need to join standard android's tables (like contacts and call log) using SQL. It is possible using the rawQuery or query methods of SQLiteDatabase class. But for the methods to work properly I need to know table names that I can provide in a raw SQL query.
Example. I want to execute query like this:
SELECT * FROM Contacts as c INNER JOIN Call_Log as l ON c.number=l.number
I know how to get field names (like CallLog.Calls.NUMBER), but I don't know how to get the name of a standard table that every android has. It is possible to hardcode the name, but the way with something like CallLog.TABLE_NAME looks much more reliable. So, where can I find an analogue of CallLog.TABLE_NAME?
Your asking for a lot of info, but this is a good summation of how to access the contacts table and how to create your own SQL table and update it with information you get from other tables.
To do any type of search of the Contacts Provider, your app must have READ_CONTACTS permission. To request this, add this element to your manifest file as a child element of :
<uses-permission android:name="android.permission.READ_CONTACTS" />
To do any type of search of the Call Log, your app must have READ_CALL_LOG permission. To request this, add this element to your manifest file as a child element of :
<uses-permission android:name="android.permission.READ_CALL_LOG" />
Code below on how to access Phone Call History
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going.
This technique tries to match a search string to the name of a contact or contacts in the Contact Provider's ContactsContract.Contacts table. You usually want to display the results in a ListView, to allow the user to choose among the matched contacts.
Saving data to a database is ideal for repeating or structured data, such as contact information. This class assumes that you are familiar with SQL databases in general and helps you get started with SQLite databases on Android. The APIs you'll need to use a database on Android are available in the android.database.sqlite package.
One of the main principles of SQL databases is the schema: a formal declaration of how the database is organized. The schema is reflected in the SQL statements that you use to create your database. You may find it helpful to create a companion class, known as a contract class, which explicitly specifies the layout of your schema in a systematic and self-documenting way.
A contract class is a container for constants that define names for URIs, tables, and columns. The contract class allows you to use the same constants across all the other classes in the same package. This lets you change a column name in one place and have it propagate throughout your code.
A good way to organize a contract class is to put definitions that are global to your whole database in the root level of the class. Then create an inner class for each table that enumerates its columns.
public final class FeedReaderContract {
// To prevent someone from accidentally instantiating the contract class,
// make the constructor private.
private FeedReaderContract() {}
/* Inner class that defines the table contents */
public static class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
}
}
Once you have defined how your database looks, you should implement methods that create and maintain the database and tables. Here are some typical statements that create and delete a table:
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
FeedEntry._ID + " INTEGER PRIMARY KEY," +
FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_SUBTITLE + TEXT_TYPE + " )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
Just like files that you save on the device's internal storage, Android stores your database in private disk space that's associated application. Your data is secure, because by default this area is not accessible to other applications.
A useful set of APIs is available in the SQLiteOpenHelper class. When you use this class to obtain references to your database, the system performs the potentially long-running operations of creating and updating the database only when needed and not during app startup. All you need to do is call getWritableDatabase() or getReadableDatabase().
To use SQLiteOpenHelper, create a subclass that overrides the onCreate(), onUpgrade() and onOpen() callback methods. You may also want to implement onDowngrade(), but it's not required.
For example, here's an implementation of SQLiteOpenHelper that uses some of the commands shown above:
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
To access your database, instantiate your subclass of SQLiteOpenHelper:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
Put Information into a Database
Insert data into the database by passing a ContentValues object to the insert() method:
// Gets the data repository in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle);
// Insert the new row, returning the primary key value of the new row
long newRowId = db.insert(FeedEntry.TABLE_NAME, null, values);
The first argument for insert() is simply the table name.
The second argument tells the framework what to do in the event that the ContentValues is empty (i.e., you did not put any values). If you specify the name of a column, the framework inserts a row and sets the value of that column to null. If you specify null, like in this code sample, the framework does not insert a row when there are no values.
To read from a database, use the query() method, passing it your selection criteria and desired columns. The method combines elements of insert() and update(), except the column list defines the data you want to fetch, rather than the data to insert. The results of the query are returned to you in a Cursor object.
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
FeedEntry._ID,
FeedEntry.COLUMN_NAME_TITLE,
FeedEntry.COLUMN_NAME_SUBTITLE
};
// Filter results WHERE "title" = 'My Title'
String selection = FeedEntry.COLUMN_NAME_TITLE + " = ?";
String[] selectionArgs = { "My Title" };
// How you want the results sorted in the resulting Cursor
String sortOrder =
FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";
Cursor c = db.query(
FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
To look at a row in the cursor, use one of the Cursor move methods, which you must always call before you begin reading values. Generally, you should start by calling moveToFirst(), which places the "read position" on the first entry in the results. For each row, you can read a column's value by calling one of the Cursor get methods, such as getString() or getLong(). For each of the get methods, you must pass the index position of the column you desire, which you can get by calling getColumnIndex() or getColumnIndexOrThrow(). For example:
cursor.moveToFirst();
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(FeedEntry._ID)
);

can i dynamically add new column to sqlite table by calling a method from another class?

I have a table in database, each record of this table needs to store
multiple Strings, i dont know how many Strings because its decided at runtime.
I want to add image uri's in database table dynamically, user
dynamically add images in my app as many as he want so i need to save
uri of them, what is the right approach to do it?
I am trying something like this by follow this Insert new column into table in sqlite ?
String ColumnName=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + "image1.jpg");
addNewColumn(ColumnName);
i used below method for this (not worked):-
First i am adding new column in table :-
public Cursor addColumn(String name){
db=dbhelper.getWritableDatabase();
return db.rawQuery("alter table info add column " + name + " text", null);
}
Then insert uri into this
public Boolean setUri(String columnName,String uri) {
ContentValues cv= new ContentValues();
cv.put(columnName,uri);
SQLiteDatabase db =dbhelper.getWritableDatabase();
long id=db.insert("info",null,cv);
if(id>-1)
return true;
else
return false;
}
is the above approach correct?
also i searched and fine below code :-
private static final String ALTER = "ALTER TABLE user_table ADD user_street1 TEXT";
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(ALTER);
}
can i call onUpgrade() method dynamically and add new column or any other way to do it..?
Thanks in advance :)
It's very bad to have an arbitrary number of columns in a table. You should instead use a second table with a foreign key referencing the ID of a row in the first table.
table user
_id username ...
------------------------
1 abc
2 xyz
table photoInfo
userId photoUri
-------------------------------
1 /path/to/image1.jpg
1 /path/to/image2.jpg
2 /path/to/image3.jpg
1 /path/to/image4.jpg
To show photos for a particular user, use a JOIN.

SQLiteOpenHelper problems

I have been working with Android and SQLite Database to develop an application.
But I am having a few problems.
This is my code:
SQLiteHelperMoney SQLiteHelperMoney1 = new SQLiteHelperMoney(this, null, null, 1);
SQLiteDatabase database = SQLiteHelperMoney1.getReadableDatabase();
Cursor cursorCategory = database.query(false, "CATEGORY", null, null, null, null, null, null, null);
SQLiteHelperMoney SQLiteHelperMoney2 = new SQLiteHelperMoney(this, null, null, 1);
SQLiteDatabase database2 = SQLiteHelperMoney2.getReadableDatabase();
Cursor cursorTransaction = database2.query(false, "TRANSACTIONS", null, null, null, null, null, null, "10");
Now the problem is, that in my cursor cursorCategory I get the data that is stored in the Category table of my database. But I do not get any data in the cursorTransaction, why does this happen??
Furthermore, while instantiating my SQLiteHelperMoney2 object, if I change the instantiation of my SQLiteHelperMoney2 object to this :
SQLiteHelperMoney SQLiteHelperMoney2 = new SQLiteHelperMoney(this, "TRANSACTIONS", null, 1);
then I get the data from the Transaction table, in my cursorTransaction.
But why does this happen?? According to the documentation, in the constructor of the SQLiteOpenHelper, the second parameter, is the name of the database, not the name of the table.
Then why does giving the name of the TABLE, in the field NAME OF DATABASE, gives the correct answer?
Furthermore, the documentation says that in the constructor for SQLiteOpenHelper, in the field "NAME" you can specify null if the database is already in the memory. I was assuming that the database had been created in the app itself, so it will be in memory only, that is why I specify the second parameter as null here :
SQLiteHelperMoney SQLiteHelperMoney1 = new SQLiteHelperMoney(this, null, null, 1);
But it works here but not in the other case.
Further, what is the name of database created by default? Because I haven't specified it in my onCreate function of the SQLiteOpenHelper. And can I change the name of my database??
EDIT:
onCreate method:
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE CATEGORY (_ID INTEGER PRIMARY KEY, NAME TEXT)");
db.execSQL("CREATE TABLE TRANSACTIONS (_ID INTEGER PRIMARY KEY, AMOUNT INTEGER, CATEGORY TEXT, NOTE TEXT, TIME TEXT, EXPENSE INTEGER)");
db.execSQL("CREATE TABLE BUDGET (_ID INTEGER PRIMARY KEY, AMOUNT INTEGER, TIME TEXT)");
ContentValues cv = new ContentValues();
cv.put("NAME", "FOOD");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "TRAVEL");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "INCOME");/*INCOME*/
db.insert("CATEGORY", null, cv);
cv.put("NAME", "TRANSPORTATION");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "MOVIE");
db.insert("CATEGORY", null, cv);
}
#laalto: I read your answer here : When is SQLiteOpenHelper onCreate() / onUpgrade() run?
And I'm a bit confused about the on-disk and in-memory database. In my onCreate method, I add rows in my Category table, because I believed that the onCreate method is only called once in the lifetime of the application, i.e., when it is run the first time, for creating the databases.
That is why in my onCreate method I add data into my CATEGORY table, because I wanted it to have some initial data.
And I believed when we make substituent calls to getReadableDatabase() and getWritableDatabase() it returns us a copy of the earlier created database for us to work with, or probably a handle to the original database and on completion saves those changes. But, after reading your answer it seems that the onCreate methods are run every time. If they are run everytime where does the data get stored, since we will loose the database when we close it??
Now the problem is, that in my cursor cursorCategory I get the data that is stored in the Category table of my database. But I do not get any data in the cursorTransaction, why does this happen??
The query returns no data from in the in-memory database you just created.
Furthermore, while instantiating my SQLiteHelperMoney2 object, if I change the instantiation of my SQLiteHelperMoney2 object to this :
...
But why does this happen??
The query returns some data from the on-disk database that has been previously created.
For some coincidence the name of the database file is the same as the table. Uninstall and reinstall your app, or clear its data in the app manager to get rid of possible old database files possibly still around.
Furthermore, the documentation says that in the constructor for SQLiteOpenHelper, in the field "NAME" you can specify null if the database is already in the memory. I was assuming that the database had been created in the app itself, so it will be in memory only
Passing a null creates a new in-memory database. It means a new, empty database that is not persisted to disk. When the database is closed, the data is gone. If you create another in-memory database, it won't see the data in other in-memory databases.
But it works here but not in the other case.
Possibly because the other helper populates the database in its onCreate() and the other doesn't.
Further, what is the name of database created by default?
Nothing. In-memory databases have no name. Named databases get stored on disk with the name you gave.

how to delete rows in a table on database using android sqlite3

i'm unable to delete row in a table using sqlite 3.In my code i would like to compare two
values an then delete the data in a table but it is not possible please help me.
while(authCur.moveToNext())
{
db.delete("auth_tab",authCur.getString(0)+"=?" , new String[] { user });
db.delete("auth_tab", null, null);
}
Deleting data
Once data is no longer needed it can be removed from the database with the delete() method. The delete() method expects 3 parameters, the database name, a WHERE clause, and an argument array for the WHERE clause. To delete all records from a table pass null for the WHERE clause and WHERE clause argument array.
db.delete("auth_tab", "authCur.getString(0)=?", new String[] {user);
Simply call the delete() method to remove records from the SQLite database. The delete method expects, the table name, and optionally a where clause and where clause argument replacement arrays as parameters. The where clause and argument replacement array work just as with update where ? is replaced by the values in the array.
or
public void deleteContact(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(auth_tab, KEY_USER + " = ?",
new String[] { user) });
db.close();
}
WATCH more, and you tube video, how to delete.

onCreate not being called after getWritableDatabase/getReadableDatabase

My app's got a database with three tables in it: one to store the names of the people it tracks, one to track an ongoing event, and one - for lack of a better term - for settings.
I load the first table when the app starts. I ask for a readable database to load in members to display, and later I write to the database when the list changes. I've had no problems here.
The other two tables, however, I can't get to work. The code in the helper classes is identical with the exception of class names and column names, and (at least until the point where I try to access the table) the code to use the table is nearly identical as well.
Here's the code for my helper class (I've got a separate helper for each table, and as I said, it's identical except for class names and columns):
public class db_MembersOpenHelper extends SQLiteOpenHelper
{
public static final String TABLE_NAME = "members_table";
public static final String[] COLUMN_NAMES = new String[] {
Constants.KEY_ID,
"name",
"score"
};
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_NAMES[0] + " INTEGER PRIMARY KEY autoincrement, "
+ COLUMN_NAMES[1] + " TEXT, "
+ COLUMN_NAMES[2] + " INTEGER);";
public db_MembersOpenHelper(Context context)
{
super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) { db.execSQL(TABLE_CREATE); }
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion + " to " + newVersion + ".");
// Do nothing. We do not have any updated DB version
}
}
Here's how I use it successfully:
db_MembersOpenHelper membersDbHelper = new db_MembersOpenHelper(this);
SQLiteDatabase membersDb = membersDbHelper.getReadableDatabase();
Cursor membersResult = membersDb.query(TABLE_NAME, null, null, null, null, null, null);
members = new HashMap<String, Integer>();
membersResult.moveToFirst();
for(int r = 0; r < membersResult.getCount(); r++)
{
members.put(membersResult.getString(1), membersResult.getInt(2));
membersResult.moveToNext();
}
membersDb.close();
And here's where it fails:
db_PlayersOpenHelper playersDbHelper = new db_PlayersOpenHelper(this);
final SQLiteDatabase playersDb = playersDbHelper.getWritableDatabase();
if(newGame)
{
for(String name : players)
{
ContentValues row = new ContentValues();
row.put(COLUMN_NAMES[1], name);
row.put(COLUMN_NAMES[2], (Integer)null);
playersDb.insert(TABLE_NAME, null, row);
}
}
The first one works like a charm. The second results in ERROR/Database(6739): Error inserting achievement_id=null name=c
android.database.sqlite.SQLiteException: no such table: players_table: , while compiling: INSERT INTO players_table(achievement_id, name) VALUES(?, ?);
...
I did do some testing, and the onCreate method is not being called at all for the tables that aren't working. Which would explain why my phone thinks the table doesn't exist, but I don't know why the method isn't getting called.
I can't figure this out; what am I doing so wrong with the one table that I accidentally did right with the other?
I think the problem is that you are managing three tables with with three helpers, but only using one database. SQLiteOpenHelper manages on database, not one table. For example, it checks to see whether the database, not table, exists when it starts. It already does, so onCreate() does not fire.
I would manage all tables with one helper.
Let me see if I get this right. You are trying to create one database with three tables. But when you create the database, you create just one table; you are somehow instantiating the same database at a different place and wonder why its onCreate method doesn't get called. Is this a correct interpretation?
My strategy would be to try and create all three tables in the single onCreate() method.
If you are working with multiple tables, then you have to create all of the tables at once. If you have run your application first and later you update your database, then it will not upgrade your DB.
Now delete your application, then run it again.
There is one more solution but it is not proper. You can declare onOpen method in which you can call onCreate. And add IF NOT EXISTS before table name in your create table string. – Sourabh just now edit

Categories

Resources