Android SQLite issue - table ... has no column named - android

I'm getting this error -
07-03 12:29:18.643: E/SQLiteLog(5181): (1) table accounts has no column named otherNotes
This is my code:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "accountsManager";
private static final String TABLE_ACCOUNTS = "accounts";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_USERID = "userId";
private static final String KEY_PASSWORD = "password";
private static final String KEY_LOGINURL = "loginUrl";
private static final String KEY_OTHERNOTES = "otherNotes";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE " + TABLE_ACCOUNTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
+ KEY_USERID + " TEXT," + KEY_PASSWORD + " TEXT," + KEY_LOGINURL + " TEXT,"
+ KEY_OTHERNOTES + " TEXT" + ");";
db.execSQL(CREATE_ACCOUNTS_TABLE);
}
public void addAccount(AccountDetails account) {
SQLiteDatabase db = this.getWritableDatabase();
System.out.println("Hello!");
ContentValues values = new ContentValues();
values.put(KEY_TITLE, account.getTitle()); // Account Title
values.put(KEY_USERID, account.getUserId()); // account userid
values.put(KEY_PASSWORD, account.getPassword()); // account password
values.put(KEY_LOGINURL, account.getLoginUrl()); // account loginurl
values.put(KEY_OTHERNOTES, account.getOtherNotes()); // account othernotes
Log.v("title", KEY_TITLE);
// Inserting Row
db.insert(TABLE_ACCOUNTS, null, values);
db.close(); // Closing database connection
}
Also, when I remove the following statement:
values.put(KEY_OTHERNOTES, account.getOtherNotes()); // account othernotes
Then I get the same problem with password...etc.
i.e, (1) table accounts has no column named password
Please help!!

It seems that you added some columns later in the database. I do agree with Ken Wolf and you should consider uninstalling and re-installing your app. One better approach is, drop and recreate all tables in onUpdate method, and increase the db version every time you change the schema.

Well, If you are confindent about syntax for creating table, than it may happen
when you add new column in your same table, for that...
1) Unistall from your device and run it again.
OR
2) Setting -> app -> ClearData
OR
3) Change DATABASE_NAME in your "DatabaseHandler" class
( I faced same problem. But I suuceed by changing DATABASE_NAME.)
OR
4) Change DATABASE_VERSION in your "DatabaseHandler" class
(If you have added new column than it will upgrade autimatically)
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

As Benil says in a comment above, change the database version number and run the code again.

The SQL code looks fine.
I think that you forgot to call open() on your database object that you created.
add this methods to you SQL class:
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public DataBaseMain open() throws SQLException{
// Open the database to make her writeable, must be called before writing
// to database
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
// Closing the database for writing, avoids error.
ourHelper.close();
}
And use when you want to call you DB.

Try "Clear Data " of your App from Settings.
For me this problem was occuring because i was storage data on SD Card and later i added some columns.
So if you are saving on SD card delete the previous data.

For those who have similar problem and above solution is not worked then check 2 things:
Space between column name and data type like
COLUMN_NUMBER+" TEXT PRIMARY KEY) not COLUMN_NUMBER+"TEXT PRIMARY KEY)
The order of column during the creation of table should same as map for data insertion like
ContentValues values = new ContentValues();
values.put(TEST_ID, testId);
values.put(CLASS_NAME, className);
values.put(SUBJECT, subject);
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TEST_ID + " INTEGER,"
+ CLASS_NAME + " TEXT,"
+ SUBJECT + " TEXT,"

Here It is your query..TRY this.
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE "+TABLE_ACCOUNTS+"(KEY_ID INTEGER PRIMARY KEY,KEY_TITLE TEXT,KEY_USERID TEXT,KEY_PASSWORD TEXT,KEY_LOGINURL TEXT,KEY_OTHERNOTES TEXT);";

It's possible if you made a mistake in creation query and then fix it,
So in the file system you have a db, but this db possibly has no such column.
Solution:
in emulator find a db file: data/data/com.somecompany.yourapp/databases/db and remove it, then try again.
It's also possible to open this file in some sql explorer and check if there is that column.

I ran into the same issue, as I updated my database table in sqlite with new columns during testing phase.
Besides the answers that were already given above, what I found really useful to update SQLite databases (e.g. after changes) is ABD Idea, an AndroidStudio plugin that allows you to:
Uninstall App
List item
Kill App
Start App
Restart App
Clear App Data
Clear App Data and Restart

You can just specify the version like this:
private static final int VERSION = 4;

If your sure your codes are OKAY, just try uninstalling the application, then rerun it agian. This solved my issue. Hope it helped

the possible solution also (not the best one) to use different version of DB, like:
public static final int DATABASE_VERSION = 2;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
it works for me.

clearing app data from the phone settings worked in my case.
either in the android emulator, you can do the same.

Current code is
override fun onCreate(db: SQLiteDatabase?) {
val createPWDTable =
"CREATE TABLE IF NOT EXISTS $tblPassword ($pwdId INTEGER PRIMARY KEY autoincrement,$title TEXT,$email TEXT,$pwd TEXT,$notes TEXT);"
db!!.execSQL(createPWDTable)
}
Now add colmn encrypted
override fun onCreate(db: SQLiteDatabase?) {
val createPWDTable =
"CREATE TABLE IF NOT EXISTS $tblPassword ($pwdId INTEGER PRIMARY KEY autoincrement,$title TEXT,$email TEXT,$pwd TEXT,$encrypted INT,$notes TEXT);"
db!!.execSQL(createPWDTable)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
if (newVersion == 2) {
val query = "ALTER TABLE $tblPassword ADD COLUMN $encrypted INT DEFAULT '0'"
db!!.execSQL(query)
}
}
Here if New user then oncreate method directly create table with encrypted value.
if Old user then onUpgrade method has check db version and alter the table

I know i have replie to this question to late but maybe someone can solve her problem with this answer.
You are put some data later in table database so the best solution is to re name your data base name.

In my case there was a query syntax error (need space b/w column name and datatype ..see attached image) in onCreate() method
to resolve it ..
I followed these steps
Correct your syntax error
change DB version number
Clear App data from settings
Uninstall and Run Your app again.]1]1
Hope it helps

Uninstall your app and reinstall your app and after that it will work fi

Related

connecting two tables with foreign keys in sqlite

Hi in my senario there is a table with 4 columns and im trying to create another table with connection to the first table but i dont whay im getting this error in logcat
2019-10-28 01:04:00.853 29812-29812/com.test.fastfoodfinder E/SQLiteDatabase: Error inserting notes_main=testeststststststststs
android.database.sqlite.SQLiteException: no such table: notes (code 1 SQLITE_ERROR): , while compiling: INSERT INTO notes(notes_main) VALUES (?)
so i have created a class for my data base and this is what i have done
public class RestaurantDBHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "FastFood_DataBase.db";
private final static int DATABASE_VERSION = 1;
private final static String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
"(" + COLUMN_RESTAURANT_ID + " INTEGER PRIMARY KEY ," +
COLUMN_RESTAURANT_NAME + " TEXT, " +
COLUMN_RESTAURANT_ADDRESS + " TEXT, " +
COLUMN_RESTAURANT_TYPE + " INTEGER, " +
COLUMN_RESTAURANT_IMAGE + " INTEGER);";
private final static String CREATE_TABLE_NOTES = "CREATE TABLE " + TABLE_NAME_NOTES +
"(" + COLUMN_NOTES_ID + " INTEGER PRIMARY KEY, "
+ COLUMN_NOTES + " TEXT," + "FOREIGN KEY (" + COLUMN_NOTES_ID + ") REFERENCES " + TABLE_NAME +"(restaurant_id) ON DELETE CASCADE)";
public final static String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
public RestaurantDBHelper(#Nullable Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("PRAGMA FOREIGN_KEYS = ON;");
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_TABLE_NOTES);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DELETE_TABLE);
onCreate(db);
}
public void addRestaurant(Restaurant restaurant) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_RESTAURANT_NAME, restaurant.getName());
values.put(COLUMN_RESTAURANT_ADDRESS, restaurant.getAddress());
values.put(COLUMN_RESTAURANT_TYPE,restaurant.getType());
values.put(COLUMN_RESTAURANT_IMAGE, restaurant.getType());
db.insert(TABLE_NAME, null, values);
db.close();
}
public void addNotes (Restaurant restaurant) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NOTES,restaurant.getNote());
db.insert(TABLE_NAME_NOTES,null,values);
db.close();
}
and
public class RestaurantContract {
public static class EntryRestaurants {
public final static String TABLE_NAME = "restaurants";
public final static String COLUMN_RESTAURANT_ID = "restaurant_id";
public final static String COLUMN_RESTAURANT_NAME = "restaurant_name";
public final static String COLUMN_RESTAURANT_ADDRESS = "restaurant_address";
public final static String COLUMN_RESTAURANT_TYPE = "restaurant_type";
public final static String COLUMN_RESTAURANT_IMAGE = "restaurant_image_type";
public final static String COLUMN_RESTAURANT_NOTE_ID = "note_id";
public final static String TABLE_NAME_NOTES = "notes";
public final static String COLUMN_NOTES_ID = "notes_id";
public final static String COLUMN_NOTES = "notes_main";
public final static int RESTAURANT_TYPE_DELIVERY = 1;
public final static int RESTAURANT_TYPE_SITDOWN = 2;
public final static int RESTAURANT_TYPE_TAKEAWAY = 3;
}
}
im kind a new in android so any help would be appreciated,thanks
I believe that your issue is with the onCreate method. This ONLY runs when the database is created, it does not run every time the App is run.
The easiest solution, assuming that you do not need to keep any existing data, is to either delete the App's data or to uninstall the App. After doing either rerun the App and the new table will be created as the onCreate method will then run.
Furthermore it is no use turning FOREIGN KEYS on in the onCreate method. FOREIGN KEYS need to be turned on every time the App is run. To fix this, override the onConfigure method and then use db.setForeignKeyConstraintsEnabled(true);
this is just a convenient alternative to using db.execSQL("PRAGMA FOREIGN_KEYS = ON;");, so if you prefer you could use this when overriding the onConfigure method.
e.g. add this method to the RestaurantDBHelper class :-
#Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
However, you will then have issues when trying to add notes as the child will be set to null and thus their will not be a link/map/association/reference between the added note and the restaurant.
You need to use something like :-
public long addNote(String note, long restaurantId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NOTES,note);
values.put(COLUMN_NOTES_ID,restaurantId);
return db.insert(TABLE_NAME_NOTES,null,values);
}
BUT then you may then have an issue as to determining the id of the restaurant.
BUT then you may then encounter a further issue in that you could only have one note per restaurant as the column used to reference the restaurant is defined as INTEGER PRIMARY KEY and is therefore is a UNIQUE column (the same value can only be used once (an exception is null as a null is considered to be unique to another null)).
If the requirement is for one note per restaurant then there is no need for the separate table the relationship is a one to one relationship so the value can be stored in the restaurant table.
If you want a restaurant to have multiple notes (one to many relationship) then you should not make the column INTEGER PRIMARY KEY, INTEGER would suffice. Then a number of notes could reference the same restaurant.
If you wanted a note to be able to be be applied to a number of restaurants then you'd use a third mapping/line/reference/associative table (other name probably also exist). Such a table would have two columns one to reference the restaurant and the other to reference the note. You would then have a many to many relationship between restaurants and notes (a note could be used by many restaurants and a restaurant could use many notes).
You may find The 3 Types of Relationships in Database Design helpful.
You have enabled foreign key constraints and thus must have a unique primary key for the foreign key to reference.
https://sqlite.org/foreignkeys.html#fk_indexes
says
If the database schema contains foreign key errors that require looking at more than one table definition to identify, then those errors are not detected when the tables are created. Instead, such errors prevent the application from preparing SQL statements that modify the content of the child or parent tables in ways that use the foreign keys. Errors reported when content is changed are "DML errors" and errors reported when the schema is changed are "DDL errors". So, in other words, misconfigured foreign key constraints that require looking at both the child and parent are DML errors. The English language error message for foreign key DML errors is usually "foreign key mismatch" but can also be "no such table" if the parent table does not exist. Foreign key DML errors are reported if:
The parent table does not exist, or
The parent key columns named in the foreign key constraint do not exist, or
The parent key columns named in the foreign key constraint are not the primary key of the parent table and are not subject to a unique constraint using collating sequence specified in the CREATE TABLE, or
The child table references the primary key of the parent without specifying the primary key columns and the number of primary key columns in the parent do not match the number of child key columns.
Given that you never give the restaurant_id a value when you inserting a restaurant then the primary key of the restaurants is probably always null and thus not unique
(Yes according to https://www.sqlitetutorial.net/sqlite-primary-key/ to make the current version of SQLite compatible with the earlier version, SQLite allows the primary key column to contain NULL values. )
So I would say the solution is to create restaurant entries with a unique primary key value when you insert a restaurant or get the database to generate a unique value creating the the restaurant table with the line:-
private final static String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
"(" + COLUMN_RESTAURANT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
....
One way to confirm this is download FastFood_DataBase.* files using Device Explorer in your app's database directory and then open it up in https://sqlitebrowser.org/ on your computer to confirm the contents.

android.database.sqlite.SQLiteException: table dataentry has no column named mrsignedname (code 1): , in android [duplicate]

I'm getting this error -
07-03 12:29:18.643: E/SQLiteLog(5181): (1) table accounts has no column named otherNotes
This is my code:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "accountsManager";
private static final String TABLE_ACCOUNTS = "accounts";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_USERID = "userId";
private static final String KEY_PASSWORD = "password";
private static final String KEY_LOGINURL = "loginUrl";
private static final String KEY_OTHERNOTES = "otherNotes";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE " + TABLE_ACCOUNTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
+ KEY_USERID + " TEXT," + KEY_PASSWORD + " TEXT," + KEY_LOGINURL + " TEXT,"
+ KEY_OTHERNOTES + " TEXT" + ");";
db.execSQL(CREATE_ACCOUNTS_TABLE);
}
public void addAccount(AccountDetails account) {
SQLiteDatabase db = this.getWritableDatabase();
System.out.println("Hello!");
ContentValues values = new ContentValues();
values.put(KEY_TITLE, account.getTitle()); // Account Title
values.put(KEY_USERID, account.getUserId()); // account userid
values.put(KEY_PASSWORD, account.getPassword()); // account password
values.put(KEY_LOGINURL, account.getLoginUrl()); // account loginurl
values.put(KEY_OTHERNOTES, account.getOtherNotes()); // account othernotes
Log.v("title", KEY_TITLE);
// Inserting Row
db.insert(TABLE_ACCOUNTS, null, values);
db.close(); // Closing database connection
}
Also, when I remove the following statement:
values.put(KEY_OTHERNOTES, account.getOtherNotes()); // account othernotes
Then I get the same problem with password...etc.
i.e, (1) table accounts has no column named password
Please help!!
It seems that you added some columns later in the database. I do agree with Ken Wolf and you should consider uninstalling and re-installing your app. One better approach is, drop and recreate all tables in onUpdate method, and increase the db version every time you change the schema.
Well, If you are confindent about syntax for creating table, than it may happen
when you add new column in your same table, for that...
1) Unistall from your device and run it again.
OR
2) Setting -> app -> ClearData
OR
3) Change DATABASE_NAME in your "DatabaseHandler" class
( I faced same problem. But I suuceed by changing DATABASE_NAME.)
OR
4) Change DATABASE_VERSION in your "DatabaseHandler" class
(If you have added new column than it will upgrade autimatically)
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
As Benil says in a comment above, change the database version number and run the code again.
The SQL code looks fine.
I think that you forgot to call open() on your database object that you created.
add this methods to you SQL class:
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public DataBaseMain open() throws SQLException{
// Open the database to make her writeable, must be called before writing
// to database
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
// Closing the database for writing, avoids error.
ourHelper.close();
}
And use when you want to call you DB.
Try "Clear Data " of your App from Settings.
For me this problem was occuring because i was storage data on SD Card and later i added some columns.
So if you are saving on SD card delete the previous data.
For those who have similar problem and above solution is not worked then check 2 things:
Space between column name and data type like
COLUMN_NUMBER+" TEXT PRIMARY KEY) not COLUMN_NUMBER+"TEXT PRIMARY KEY)
The order of column during the creation of table should same as map for data insertion like
ContentValues values = new ContentValues();
values.put(TEST_ID, testId);
values.put(CLASS_NAME, className);
values.put(SUBJECT, subject);
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TEST_ID + " INTEGER,"
+ CLASS_NAME + " TEXT,"
+ SUBJECT + " TEXT,"
Here It is your query..TRY this.
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE "+TABLE_ACCOUNTS+"(KEY_ID INTEGER PRIMARY KEY,KEY_TITLE TEXT,KEY_USERID TEXT,KEY_PASSWORD TEXT,KEY_LOGINURL TEXT,KEY_OTHERNOTES TEXT);";
It's possible if you made a mistake in creation query and then fix it,
So in the file system you have a db, but this db possibly has no such column.
Solution:
in emulator find a db file: data/data/com.somecompany.yourapp/databases/db and remove it, then try again.
It's also possible to open this file in some sql explorer and check if there is that column.
I ran into the same issue, as I updated my database table in sqlite with new columns during testing phase.
Besides the answers that were already given above, what I found really useful to update SQLite databases (e.g. after changes) is ABD Idea, an AndroidStudio plugin that allows you to:
Uninstall App
List item
Kill App
Start App
Restart App
Clear App Data
Clear App Data and Restart
You can just specify the version like this:
private static final int VERSION = 4;
If your sure your codes are OKAY, just try uninstalling the application, then rerun it agian. This solved my issue. Hope it helped
the possible solution also (not the best one) to use different version of DB, like:
public static final int DATABASE_VERSION = 2;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
it works for me.
clearing app data from the phone settings worked in my case.
either in the android emulator, you can do the same.
Current code is
override fun onCreate(db: SQLiteDatabase?) {
val createPWDTable =
"CREATE TABLE IF NOT EXISTS $tblPassword ($pwdId INTEGER PRIMARY KEY autoincrement,$title TEXT,$email TEXT,$pwd TEXT,$notes TEXT);"
db!!.execSQL(createPWDTable)
}
Now add colmn encrypted
override fun onCreate(db: SQLiteDatabase?) {
val createPWDTable =
"CREATE TABLE IF NOT EXISTS $tblPassword ($pwdId INTEGER PRIMARY KEY autoincrement,$title TEXT,$email TEXT,$pwd TEXT,$encrypted INT,$notes TEXT);"
db!!.execSQL(createPWDTable)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
if (newVersion == 2) {
val query = "ALTER TABLE $tblPassword ADD COLUMN $encrypted INT DEFAULT '0'"
db!!.execSQL(query)
}
}
Here if New user then oncreate method directly create table with encrypted value.
if Old user then onUpgrade method has check db version and alter the table
I know i have replie to this question to late but maybe someone can solve her problem with this answer.
You are put some data later in table database so the best solution is to re name your data base name.
In my case there was a query syntax error (need space b/w column name and datatype ..see attached image) in onCreate() method
to resolve it ..
I followed these steps
Correct your syntax error
change DB version number
Clear App data from settings
Uninstall and Run Your app again.]1]1
Hope it helps
Uninstall your app and reinstall your app and after that it will work fi

Manage DB before uploading to playstore in android

I have uploaded an application to play store couple of weeks back. This application involves sqlite database that stores information on username, password, other details that given are by user while using the application locally.
Now I have couple of more tables and fields added to database and wanna upload the application to playstore as an update?
My worry is if the user updates the application from playstore - After update - all the data stored in database will be saved or will the user has to recreate everything from scratch?
Let me know!
Thanks!
You have to override the onUpgade method of SQLiteOpenHelper. In the OnUpgrade method you can either erase the data(drop sqlite command) or maintain the data with the additional columns(alter sqlite command) or create new table (create sqlite command).
Refer the following snippet.
I assume your version would be 1.(Plz check the constructor of your SqliteOpenHelper class)
Increment the version by 1.
class DatabaseHelper extends SqliteOplenHelper{
private static final int DATABASE_VERSION = 2; //new version of the database
private static final int Database_name = "MyDatabase";
private static final String alterUserName = "alter table users add name text";
private static final String table_users = "create table if not exists "
+ users + "(" + "_id integer primary key autoincrement,"
+ "email text" + ")";
public DatabaseHelper(Context context) {
super(context, Database_name, null, DATABASE_VERSION);
cntxt = context;
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(table_users);
db.execSQL(alterUserName);
}
}
Now everytime when you roll the next update with database changes be sure to increment the database version by 1 else let it remain the same.
This isn't done for you automatically. In your SQLiteOpenHelper, you need to increment the Schema integer. This will trigger the on upgrade method for your existing users.
Adding a table is not a problem, just do this in onUpgrade, nothing breaks.
However to add fields, you should use the 'ALTER TABLE' SQL command
If you add new columns you can use ALTER TABLE to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old table, then create the new table and then populate the new table with the contents of the old tab
See the official reference here

Android simple SQLite database application

I downloaded a simple login page code in zip file from
http://bit.ly/saket_github_loginpage.
Please check this application and tell me where the database file is. And how can I see this? I also want to create some new tables. How can I do this? I have tried getting it to work with the following code:
public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "saket.db";
private static final int DATABASE_VERSION = 1;
public static final String SAKET_TABLE_NAME = "login";
public static final String SAKET_TABLE_NAME1 = "Employee";
private static final String SAKET_TABLE_CREATE =
"CREATE TABLE " + SAKET_TABLE_NAME + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NO TNULL);";
private static final String SAKET_TABLE_CREATE1 =
"CREATE TABLE " + SAKET_TABLE_NAME1 + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"name TEXT NOT NULL, Address TEXT NOT NULL, email TEXT NOT NULL);";
private static final String SAKET_DB_ADMIN = "INSERT INTO" +
SAKET_TABLE_NAME+"values(1, admin, password, admin#gmail.com);";
private static final String SAKET_DB_ADMIN1 = "INSERT INTO " +
SAKET_TABLE_NAME+"values(2, emp, password, Emp#gmail.com);";
private static final String SAKET_DB_ADMIN2 = "INSERT INTO " +
SAKET_TABLE_NAME1 + "values(1, mahesh, address, mahesh#gmail.com);";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
}
It's better you check on emulator, or a rooted device....... Follow these steps
In emulator run your application.
In eclipse go to Window>Show view>Other...>Android and click file explorer
In File explorer find a folder called data which should not be on your SD card
in data>data>your.application.packagename you can find database folder, your database is there
Find an icon called pull a file from device...... you can copy that database anywhere you like using this button....
There are many clients to view and operate the SQLITE database including firefox has one..... I recommend to use this.... If you are on ubuntu they have a good application in their software center..... [I don't remember the name so apologies for that]
I hope this works for you....
1)You can see your database in file explorer. you can refer to file explorer as
Window -> show view -> other -> Select File Explorer.
in File Explorer goto
data -> data -> com.YourApplicationPackage -> YourDatabase
2)To create new table is by SQL use CREATE query having stored in String Constant and the execute it by your SQLite database object. if your sqlite database object is db then it would be db.execSQL(StringConstant); String constant carries your SQL CREATE query...
I hope you got your answer....

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