I am new to sqlite and i am trying to develop an application using sqlite, but I got stuck with an error
no such column: reminder.
I searched many sites but could not find out the problem. I do not know what all details I need to post. But I am getting error in my logcat like this.
Caused by: android.database.sqlite.SQLiteException: no such column: reminder (code 1): , while compiling: SELECT id, party_name, bank_name, reminder, expiry_date, reminder_date, cheque_number, amount FROM financeTBL ORDER BY idDESC
DatabaseHandler class
public class DatabaseHandler extends SQLiteOpenHelper {
private Context ctx;
public DatabaseHandler(Context context) {
super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
this.ctx = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ALERT_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
+ Constants.KEY_ID + " INTEGER PRIMARY KEY,"
+ Constants.KEY_PARTY_NAME + "TEXT,"
+ Constants.KEY_CHEQUE_NUMBER + "TEXT"
+ Constants.KEY_AMOUNT + "TEXT"
+ Constants.KEY_EXPIRY_DATE + "LONG"
+ Constants.KEY_REMINDER_DATE + "LONG"
+ Constants.KEY_REMINDER + "Text,"
+ Constants.KEY_BANK_NAME + "TEXT);";
db.execSQL(CREATE_ALERT_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NAME);
onCreate(db);
}
public void addfinance(Finance finance) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Constants.KEY_PARTY_NAME, finance.getPartyName());
values.put(Constants.KEY_BANK_NAME, finance.getBankName());
values.put(Constants.KEY_REMINDER, finance.getReminder());
values.put(Constants.KEY_REMINDER_DATE, finance.getReminderDate().getTime());
values.put(Constants.KEY_AMOUNT, finance.getAmount());
values.put(Constants.KEY_CHEQUE_NUMBER,finance.getChequeNumber());
values.put(Constants.KEY_EXPIRY_DATE, finance.getExpiryDate().getTime());
db.insert(Constants.TABLE_NAME, null, values);
Log.d("Saved!!", "Saved to DB");
}
public Finance getGrocery(int id) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query(Constants.TABLE_NAME, new String[] {Constants.KEY_ID,
Constants.KEY_AMOUNT,
Constants.KEY_REMINDER_DATE,
Constants.KEY_PARTY_NAME,
Constants.KEY_BANK_NAME,
Constants.KEY_REMINDER,
Constants.KEY_CHEQUE_NUMBER,
Constants.KEY_EXPIRY_DATE},
Constants.KEY_ID + "=?",
new String[] {String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Finance finance = new Finance();
finance.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Constants.KEY_ID))));
finance.setPartyName(cursor.getString(cursor.getColumnIndex(Constants.KEY_PARTY_NAME)));
finance.setBankName(cursor.getString(cursor.getColumnIndex(Constants.KEY_BANK_NAME)));
finance.setReminder(cursor.getString(cursor.getColumnIndex(Constants.KEY_REMINDER)));
finance.setChequeNumber(cursor.getString(cursor.getColumnIndex(Constants.KEY_CHEQUE_NUMBER)));
finance.setAmount(cursor.getString(cursor.getColumnIndex(Constants.KEY_AMOUNT)));
Date date= new Date(cursor.getLong(cursor.getColumnIndex(Constants.KEY_EXPIRY_DATE)));
finance.setExpiryDate(date);
date=new Date(cursor.getLong(cursor.getColumnIndex(Constants.KEY_REMINDER_DATE)));
finance.setReminderDate(date);
return finance;
}
//Get all Groceries
public List<Finance> getAllFinanceDetails() {
SQLiteDatabase db = this.getReadableDatabase();
List<Finance> financeList = new ArrayList<>();
Cursor cursor = db.query(Constants.TABLE_NAME, new String[] {
Constants.KEY_ID,
Constants.KEY_PARTY_NAME,
Constants.KEY_BANK_NAME,
Constants.KEY_REMINDER,
Constants.KEY_EXPIRY_DATE,
Constants.KEY_REMINDER_DATE,
Constants.KEY_CHEQUE_NUMBER
,Constants.KEY_AMOUNT
}, null, null, null, null, Constants.KEY_ID + "DESC");
if (cursor.moveToFirst()) {
do {
Finance finance = new Finance();
finance.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Constants.KEY_ID))));
finance.setPartyName(cursor.getString(cursor.getColumnIndex(Constants.KEY_PARTY_NAME)));
finance.setBankName(cursor.getString(cursor.getColumnIndex(Constants.KEY_BANK_NAME)));
finance.setReminder(cursor.getString(cursor.getColumnIndex(Constants.KEY_REMINDER)));
Date date= new Date(cursor.getLong(cursor.getColumnIndex(Constants.KEY_EXPIRY_DATE)));
finance.setExpiryDate(date);
date=new Date(cursor.getLong(cursor.getColumnIndex(Constants.KEY_REMINDER_DATE)));
finance.setReminderDate(date);
finance.setChequeNumber(cursor.getString(cursor.getColumnIndex(Constants.KEY_CHEQUE_NUMBER)));
finance.setAmount(cursor.getString(cursor.getColumnIndex(Constants.KEY_AMOUNT)));
financeList.add(finance);
}while (cursor.moveToNext());
}
return financeList;
}
Constants class
public class Constants {
public static final int DB_VERSION = 1;
public static final String DB_NAME = "financeDB";
public static final String TABLE_NAME = "financeTBL";
public static final String KEY_ID = "id";
public static final String KEY_PARTY_NAME = "party_name";
public static final String KEY_BANK_NAME = "bank_name";
public static final String KEY_REMINDER = "reminder";
public static final String KEY_CHEQUE_NUMBER ="cheque_number";
public static final String KEY_AMOUNT = "amount";
public static final String KEY_EXPIRY_DATE = "expiry_date";
public static final String KEY_REMINDER_DATE = "reminder_date";
}
You're missing some commas from your CREATE TABLE statement, which means they'll be seen as one column rather than separate.
String CREATE_ALERT_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
+ Constants.KEY_ID + " INTEGER PRIMARY KEY,"
+ Constants.KEY_PARTY_NAME + "TEXT,"
+ Constants.KEY_CHEQUE_NUMBER + "TEXT" // <-- HERE
+ Constants.KEY_AMOUNT + "TEXT" // <-- HERE
+ Constants.KEY_EXPIRY_DATE + "LONG" // <-- HERE
+ Constants.KEY_REMINDER_DATE + "LONG" // <-- HERE
+ Constants.KEY_REMINDER + "Text,"
+ Constants.KEY_BANK_NAME + "TEXT);";
You're also missing spaces before the column type, so for example the reminder column will appear as reminderText in the constructed statement. I'd recommend reformatting your CREATE TABLE statement as so:
String CREATE_ALERT_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + " ("
+ Constants.KEY_ID + " INTEGER PRIMARY KEY,"
+ Constants.KEY_PARTY_NAME + " TEXT,"
+ Constants.KEY_CHEQUE_NUMBER + " TEXT,"
+ Constants.KEY_AMOUNT + " TEXT,"
+ Constants.KEY_EXPIRY_DATE + " LONG,"
+ Constants.KEY_REMINDER_DATE + " LONG,"
+ Constants.KEY_REMINDER + " TEXT,"
+ Constants.KEY_BANK_NAME + " TEXT);";
One more space you need to add is in the ORDER BY part of this query:
Constants.KEY_ID + "DESC"
needs to be:
Constants.KEY_ID + " DESC"
In your onCreate You are forgetting commas . try this
public void onCreate(SQLiteDatabase db) {
String CREATE_ALERT_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
+ Constants.KEY_ID + " INTEGER PRIMARY KEY,"
+ Constants.KEY_PARTY_NAME + " TEXT,"
+ Constants.KEY_CHEQUE_NUMBER + " TEXT,"
+ Constants.KEY_AMOUNT + " TEXT,"
+ Constants.KEY_EXPIRY_DATE + " LONG,"
+ Constants.KEY_REMINDER_DATE + " LONG,"
+ Constants.KEY_REMINDER + " Text,"
+ Constants.KEY_BANK_NAME + " TEXT);";
db.execSQL(CREATE_ALERT_TABLE);
}
Related
Hi guys so here is my problem, I need to be able to access the unique ID of a row that is created because I created a second table that will "Connected" through the IDs. However here is my issue, whenever I try calling this method, my app crashes.. I was hoping the community could help me out here. Many Thanks.
public void addExerciseToDatabase(Exercises exercises){
SQLiteDatabase db = myDBHelper.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues nuValues = new ContentValues();
values.put(myDBHelper.COLUMN_BODYPARTNAME, exercises.get_bodyPart());
values.put(myDBHelper.COLUMN_EXERCISENAME, exercises.get_exerciseName());
long ID = db.insert(myDBHelper.TABLE_EXERCISES, null, values);
//FOR THE NEW TABLE VALUES
nuValues.put(myDBHelper.COLUMN_EXERCISENAME_ID, ID);
nuValues.put(myDBHelper.COLUMN_NUMSETS, exercises.get_numSets());
nuValues.put(myDBHelper.COLUMN_NUMWEIGHT, exercises.get_numWeight());
nuValues.put(myDBHelper.COLUMN_NUMREPS, exercises.get_numReps());
db.insert(myDBHelper.TABLE_EXERCISES_VALUE, null, nuValues);
db.close();
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THIS method returns the EXERCISE ID, that corresponds to the exercise
passed and Bodypart passed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
public int getExerciseID(String exercise, String bodyPart) {
SQLiteDatabase db = myDBHelper.getWritableDatabase();
String query = "SELECT " + myDBHelper.COLUMN_ID + " FROM " + myDBHelper.TABLE_EXERCISES + " WHERE " +
myDBHelper.COLUMN_BODYPARTNAME + " = " + bodyPart + " AND " +
myDBHelper.COLUMN_EXERCISENAME + " = " + exercise ;
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
int exerciseID = Integer.parseInt(c.getString(0));
/*int exerciseID = c.getInt(c.getColumnIndex(myDBHelper.COLUMN_ID));*/
c.close();
db.close();
return exerciseID;
}
HERES MY TABLE:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "exercises.db";
public static final String TABLE_EXERCISES = "exercises";
public static final String TABLE_EXERCISES_VALUE = "exercises_value";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_BODYPARTNAME = "bodypartname";
public static final String COLUMN_EXERCISENAME = "exercisename";
public static final String COLUMN_EXERCISENAME_ID = "exerciseid";
public static final String COLUMN_NUMSETS = "numsets";
public static final String COLUMN_NUMWEIGHT = "numweight";
public static final String COLUMN_NUMREPS = "numreps";
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_EXERCISES + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_BODYPARTNAME + " TEXT NOT NULL," +
COLUMN_EXERCISENAME + " TEXT NOT NULL" +
");";
db.execSQL(query);
query = "CREATE TABLE " + TABLE_EXERCISES_VALUE + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_EXERCISENAME_ID + " INTEGER NOT NULL," +
COLUMN_NUMSETS + " INTEGER NOT NULL," +
COLUMN_NUMWEIGHT + " INTEGER NOT NULL," +
COLUMN_NUMREPS + " INTEGER NOT NULL" +
");";
db.execSQL(query);
}
Update your query as your query not contains spaces in SELECT and FROM
String query = "SELECT " + myDBHelper.COLUMN_ID + " FROM " + myDBHelper.TABLE_EXERCISES + " WHERE " +
myDBHelper.COLUMN_BODYPARTNAME + " = " + bodyPart + " AND " +
myDBHelper.COLUMN_EXERCISENAME + " = " + exercise ;
Also can you post your logcat error.
public int getExerciseID(String exercise, String bodyPart) {
int exerciseID = 0;
SQLiteDatabase db = myDBHelper.getWritableDatabase();
String query = "SELECT " + myDBHelper.COLUMN_ID + " FROM " + myDBHelper.TABLE_EXERCISES + " WHERE " +
myDBHelper.COLUMN_BODYPARTNAME + " = " + bodyPart + " AND " +
myDBHelper.COLUMN_EXERCISENAME + " = " + exercise ;
Cursor c = db.rawQuery(query, null);
if(c.getCount()>0){
c.moveToFirst();
exerciseID = Integer.parseInt(c.getString(0));
/*exerciseID = c.getInt(c.getColumnIndex(myDBHelper.COLUMN_ID));*/
}
c.close();
db.close();
return exerciseID;
}
I had made SQLite database to my android application and it worked fine until I tried to add new column called "state". I have read previous posts in forum, and I tried adding new database version, modifying onUpgrade method, I have also tried deleting app, cleaning cache, and adding ALTER table but I still get error:
table has no column named state
So how do I update my database to make it work?
Here's my code
public class DatabaseHelper extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "dbase";
// Contacts table name
private static final String TABELL_BRUKERE = "bTab";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAVN = "navn";
private static final String KEY_ADR = "adresse";
private static final String KEY_BIL = "bil_kjennemerke";
private static final String KEY_STATE = "state";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "DROP TABLE IF EXISTS " + TABELL_BRUKERE + " CREATE TABLE " + TABELL_BRUKERE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAVN + " TEXT,"
+ KEY_ADR + " TEXT,"
+ KEY_BIL + " TEXT"
+ KEY_STATE + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String CREATE_CONTACTS_TABLE = "DROP TABLE IF EXISTS " + TABELL_BRUKERE + " CREATE TABLE " + TABELL_BRUKERE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAVN + " TEXT,"
+ KEY_ADR + " TEXT,"
+ KEY_BIL + " TEXT"
+ KEY_STATE + " TEXT" + ")";
if (oldVersion < 4 )
db.execSQL(CREATE_CONTACTS_TABLE);
// Create tables again
onCreate(db);
}
public void addContact(Bruker bruker) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAVN, bruker.getNavn()); // navn
values.put(KEY_ADR, bruker.getAdresse()); // Adresse
values.put(KEY_BIL, bruker.getBilmerke()); // Bil
values.put(KEY_STATE, bruker.getState()); // state
// Inserting Row
db.insert(TABELL_BRUKERE, null, values);
db.close(); // Closing database connection
}
public int getCount() {
String countQuery = "SELECT * FROM " + TABELL_BRUKERE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// Getting single contact
public Bruker getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABELL_BRUKERE, new String[] { KEY_ID,
KEY_NAVN, KEY_ADR,KEY_BIL,KEY_STATE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Bruker bruker = new Bruker(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4));
// return contact
return bruker;
}
// Getting All Contacts
public List<Bruker> getBrukere() {
List<Bruker> contactList = new ArrayList<Bruker>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABELL_BRUKERE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Bruker bruker = new Bruker();
bruker.setID(Integer.parseInt(cursor.getString(0)));
bruker.setNavn(cursor.getString(1));
bruker.setAdresse(cursor.getString(2));
bruker.setBilmerke(cursor.getString(3));
bruker.setState(cursor.getString(4));
// Adding contact to list
contactList.add(bruker);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Deleting single contact
public void deleteContact(Bruker bruker) {}
}
create query in Oncreate method:
" CREATE TABLE " + TABELL_BRUKERE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAVN + " TEXT,"
+ KEY_ADR + " TEXT,"
+ KEY_BIL + " TEXT,"
+ KEY_STATE + " TEXT" + ")";
and
Delete query in onupgrade method :
"DROP TABLE IF EXISTS " + TABELL_BRUKERE ;
In your onCreate and onUpdate you forget to put a "," after the KEY_BIL text.
Therefore android could not "parse" you string succesfull.
so, the CREATE_CONTACTS_TABLE string should look like this:
String CREATE_CONTACTS_TABLE = "DROP TABLE IF EXISTS " + TABELL_BRUKERE + " CREATE TABLE " + TABELL_BRUKERE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAVN + " TEXT,"
+ KEY_ADR + " TEXT,"
+ KEY_BIL + " TEXT," // add the "," here !!!
+ KEY_STATE + " TEXT" + ")";
KEY_BIL + " TEXT after you missed comma(,)
String CREATE_CONTACTS_TABLE = "DROP TABLE IF EXISTS " + TABELL_BRUKERE + " CREATE TABLE " + TABELL_BRUKERE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAVN + " TEXT,"
+ KEY_ADR + " TEXT,"
+ KEY_BIL + " TEXT,"
+ KEY_STATE + " TEXT" + ")";
So, I've created an app which SHOULD be making a database. However, I've looked at the ADM and I've found it's not in there.
My confusion is why this is it not being created? I have:
DatabaseHandler.java
MyActivity.java
Car.java
I am calling methods from the DatabaseHandler.java inside the MyActivity. I have 0 errors in my project.
Here is my DatabaseHandler.java:
package com.example.brad.myapplication;
import android.accounts.Account;
import android.database.DatabaseUtils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import static android.database.DatabaseUtils.dumpCursorToString;
/**
* Created by Brad on 19/07/2014.
*/
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "carsGrid",
TABLE_CARS = "cars",
KEY_ID = "id",
KEY_POSTCODE = "postcode",
KEY_ADDRESS = "address",
KEY_IMAGE = "image";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE" + TABLE_CARS + ")" + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ADDRESS + " TEXT,"+ KEY_POSTCODE + " TEXT," + KEY_IMAGE + " TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CARS);
onCreate(db);
}
public void createCar(Car car) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ADDRESS, car.get_address());
values.put(KEY_POSTCODE, car.get_postcode());
values.put(KEY_IMAGE, car.get_image());
db.insert(TABLE_CARS, null, values);
db.close();
}
public Car getCar(int id) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_CARS, new String[] {KEY_ID, KEY_IMAGE, KEY_ADDRESS, KEY_POSTCODE}, KEY_ID + "=?", new String[] { String.valueOf(id)}, null,null,null,null);
if (cursor != null)
cursor.moveToFirst();
Car car = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3));
return car;
}
public int getCarCount() {
//SELECT * FROM CARS
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM" + TABLE_CARS, null);
cursor.close();
return cursor.getCount();
}
public String getRandomImageKey() {
//SELECT * FROM CARS
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM" + TABLE_CARS + "ORDER BY RANDOM() LIMIT 1", null);
int columnIndex = cursor.getColumnIndex(KEY_ID);
String toReturn = cursor.getString(columnIndex);
return toReturn;
}
public Car getCurrentCar() {
SQLiteDatabase db = getWritableDatabase();
String sql = "SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM " + TABLE_CARS + "ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(sql, new String[] {});
Car car = null;
try {
if (cursor.moveToFirst()) {
car = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3));
}
}
finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
}
return car;
}
}
Here is my MyActivity.java:
public class MyActivity extends Activity {
// Defining elements in XML
EditText postCodeStringTxt, DistanceNumberTxt, DebugCarTxt;
String imagestring;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Creating elements in XML
postCodeStringTxt = (EditText) findViewById(R.id.postCodeString);
DistanceNumberTxt = (EditText) findViewById(R.id.DistanceNumber);
DebugCarTxt = (EditText) findViewById(R.id.DebugCar);
// you should instantiate 'DatabaseHandler' here
DatabaseHandler db = new DatabaseHandler(this); // "this" refer to the context
Car cars = db.getCurrentCar();
db.createCar(new Car(cars.get_id(),cars.get_address(),cars.get_postcode(),cars.get_image()));
String rows= "id : "+ cars.get_id()+ " address : "+cars.get_address() + "postcode : "+cars.get_postcode()+" image : "+cars.get_image();
How can I zone into the area which isn't working if there are no errors in the project?
Change your Create Table SQL Command with below:
db.execSQL("CREATE TABLE" + TABLE_CARS + "(" + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ADDRESS + " TEXT,"+ KEY_POSTCODE + " TEXT," + KEY_IMAGE + " TEXT)");
Your DB is not created it's because your create Table SQL Command Syntax is wrong.
You should also change this
Cursor cursor = db.rawQuery("SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM" + TABLE_CARS + "ORDER BY RANDOM() LIMIT 1", null);
to
Cursor cursor = db.rawQuery("SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM " + TABLE_CARS + " ORDER BY RANDOM() LIMIT 1", null);// make space after FROM and before ORDER BY
in your getRandomImageKey() method and also do same in getCurrentCar() method
In your DatabaseHandler class create table syntax is wrong.so change this line
db.execSQL("CREATE TABLE" + TABLE_CARS + ")" + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ADDRESS + " TEXT,"+ KEY_POSTCODE + " TEXT," + KEY_IMAGE + " TEXT)");
into
db.execSQL("CREATE TABLE" + TABLE_CARS + "(" + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ADDRESS + " TEXT,"+ KEY_POSTCODE + " TEXT," + KEY_IMAGE + " TEXT)");
As Frank suggest your two rawQuery statements are wrong give space after FROM
That too close Cursor after your opeariton is done.
Not only your CREATE TABLE is wrong, as correctly pointed out by others (a reversed bracket and a missing space):
db.execSQL("CREATE TABLE" + TABLE_CARS + ")" + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ADDRESS + " TEXT,"+ KEY_POSTCODE + " TEXT," + KEY_IMAGE + " TEXT)");
should be
db.execSQL("CREATE TABLE " + TABLE_CARS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ADDRESS + " TEXT,"+ KEY_POSTCODE + " TEXT," + KEY_IMAGE + " TEXT)");
You have another issue here (missing a space):
Cursor cursor = db.rawQuery("SELECT * FROM" + TABLE_CARS, null);
should be
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CARS, null);
Try using this:
db.execSQL("CREATE TABLE" + TABLE_CARS + "(" + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ADDRESS + " TEXT,"+ KEY_POSTCODE + " TEXT," + KEY_IMAGE + " TEXT)");
Try this..
String createTableQuery=String.format(
"CREATE TABLE %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT,%s TEXT,%s TEXT)",
TABLE_CARS, KEY_ID, KEY_ADDRESS, KEY_POSTCODE, KEY_IMAGE);
....
dB.execSQL(createTableQuery);
I'm working on a project using SQLite.
I did create my db a while ago and so on but yesterday i uninstall my app from the phone and tryed to run it again via eclipse and it seems that since that moment, it doesn't work anymore.
first the code :
my SQLHelper :
public class SQLHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "listopresto";
public static final String TABLE_CATEGORIES = "categories";
public static final String CATEGORY_NAME = "name";
public static final String CATEGORY_ID = "id";
public static final String CATEGORY_ID_PARENT = "parent_id";
public static final String CATEGORY_URL_IMAGE = "image_url";
public static final String TABLE_SHOPPING_LIST = "shopping_list";
public static final String SHOPPING_LIST_ID = "shopping_list_id";
public static final String SHOPPING_LIST_NAME = "shopping_list_name";
public static final String SHOPPING_LIST_DATE_CREATION = "shopping_list_date_creation";
public static final String TABLE_SHOPPING_LIST_ITEMS = "shopping_list_items";
public static final String SHOPPING_LIST_ITEMS_LIST_ID = "shopping_list_items_list_id";
public static final String SHOPPING_LIST_ITEMS_ID = "shopping_list_items_id";
public static final String SHOPPING_LIST_ITEMS_NB_ITEMS = "shopping_list_items_nb_items";
public static final String SHOPPING_LIST_ITEMS_CHECKED = "shopping_list_items_checked";
public static final String TABLE_INFOS = "infos";
public static final String INFOS_AGE = "age";
public static final String INFOS_MAIL = "mail";
public static final String INFOS_DISPLAY_PRICE = "display_price";
public static final String INFOS_TOKEN = "token";
public static final String INFOS_REFRESH_TOKEN = "refresh_token";
public static final String INFOS_TOKEN_EXPIRATION = "token_expiration";
public static final String INFOS_REFRESH_TOKEN_EXPIRATION = "refresh_token_expiration";
public static final String INFOS_APP_VERSION = "app_version";
public static final String TABLE_ITEMS = "items";
public static final String ITEM_ID = "id";
public static final String ITEM_NAME = "name";
public static final String ITEM_CATEGORY_ID = "item_category_id";
public static final String ITEM_PRICE = "item_price";
public SQLHelper(Context context){
super(context, DATABASE_NAME, null, 25);
}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_CATEGORIES = "CREATE TABLE " + TABLE_CATEGORIES + "(" + CATEGORY_NAME + " TEXT," + CATEGORY_ID + " INTEGER, " + CATEGORY_ID_PARENT + " INTEGER," + CATEGORY_URL_IMAGE + " TEXT" + ")" ;
String CREATE_TABLE_INFOS = "CREATE TABLE " + TABLE_INFOS + "(" + INFOS_AGE + " INTEGER," + INFOS_MAIL + " TEXT," + INFOS_DISPLAY_PRICE + " TEXT," + INFOS_TOKEN + " TEXT," + INFOS_REFRESH_TOKEN + " TEXT," + INFOS_TOKEN_EXPIRATION + " TEXT, " + INFOS_REFRESH_TOKEN_EXPIRATION + " TEXT, " + INFOS_APP_VERSION + " TEXT" + ")";
String CREATE_TABLE_ITEMS = "CREATE TABLE " + TABLE_ITEMS + "(" + ITEM_ID + " INTEGER," + ITEM_NAME + " TEXT," + ITEM_CATEGORY_ID + " INTEGER," + ITEM_PRICE + " REAL" + ")";
String CREATE_TABLE_SHOPPING_LIST = "CREATE TABLE " + TABLE_SHOPPING_LIST + "(" + SHOPPING_LIST_ID + " INTEGER," + SHOPPING_LIST_NAME + " TEXT," + SHOPPING_LIST_DATE_CREATION + " TEXT" + ")";
String CREATE_TABLE_SHOPPING_LIST_ITEMS = "CREATE TABLE " + TABLE_SHOPPING_LIST_ITEMS + "(" + SHOPPING_LIST_ITEMS_LIST_ID + " INTEGER," + SHOPPING_LIST_ITEMS_ID + " INTEGER," + SHOPPING_LIST_ITEMS_NB_ITEMS + " INTEGER," + SHOPPING_LIST_ITEMS_CHECKED + " INTEGER" + ")";
db.execSQL(CREATE_TABLE_CATEGORIES);
db.execSQL(CREATE_TABLE_INFOS);
db.execSQL(CREATE_TABLE_ITEMS);
db.execSQL(CREATE_TABLE_SHOPPING_LIST);
db.execSQL(CREATE_TABLE_SHOPPING_LIST_ITEMS);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIES);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_INFOS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SHOPPING_LIST);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SHOPPING_LIST_ITEMS);
onCreate(db);
}
/*
*
* METHODES TABLE INFOS
* z
*/
public void addInfos(InfosModel infos){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(INFOS_AGE, infos.getAge());
values.put(INFOS_MAIL, infos.getMail());
values.put(INFOS_DISPLAY_PRICE, infos.getDisplayPrice());
values.put(INFOS_TOKEN, infos.getToken());
values.put(INFOS_REFRESH_TOKEN, infos.getRefreshToken());
values.put(INFOS_TOKEN_EXPIRATION, infos.getTokenExpiration());
values.put(INFOS_REFRESH_TOKEN_EXPIRATION, infos.getRefreshTokenExpiration());
values.put(INFOS_APP_VERSION, infos.getAppVersion());
db.insert(TABLE_INFOS, null, values);
db.close();
}
public void updateInfos(InfosModel allInfos){
String Query = "UPDATE " + TABLE_INFOS +
" SET " + INFOS_AGE + "=" + allInfos.getAge() +
", " + INFOS_MAIL + "=\"" + allInfos.getMail() + "\""+
", " + INFOS_DISPLAY_PRICE + "=\"" + allInfos.getDisplayPrice() + "\"" +
", " + INFOS_TOKEN + "=\"" + allInfos.getToken() + "\"" +
", " + INFOS_REFRESH_TOKEN + "=\"" + allInfos.getRefreshToken() + "\"" +
", " + INFOS_TOKEN_EXPIRATION + "=\"" + allInfos.getTokenExpiration() + "\"" +
", " + INFOS_REFRESH_TOKEN_EXPIRATION + "=\"" + allInfos.getRefreshTokenExpiration() + "\"" +
", " + INFOS_APP_VERSION + "=\"" + allInfos.getAppVersion() + "\"";
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL(Query);
}
public InfosModel getInfos(){
String selectQuery = "SELECT * FROM " + TABLE_INFOS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null){
if (cursor.moveToFirst()){
InfosModel infos = new InfosModel();
infos.setMail(cursor.getString(cursor.getColumnIndex(INFOS_MAIL)));
infos.setAge(cursor.getInt(cursor.getColumnIndex(INFOS_AGE)));
infos.setDisplayPrice(cursor.getString(cursor.getColumnIndex(INFOS_DISPLAY_PRICE)));
infos.setToken(cursor.getString(cursor.getColumnIndex(INFOS_TOKEN)));
infos.setRefreshToken(cursor.getString(cursor.getColumnIndex(INFOS_REFRESH_TOKEN)));
infos.setTokenExpiration(cursor.getString(cursor.getColumnIndex(INFOS_TOKEN_EXPIRATION)));
infos.setRefreshTokenExpiration(cursor.getString(cursor.getColumnIndex(INFOS_REFRESH_TOKEN_EXPIRATION)));
infos.setAppVersion(cursor.getString(cursor.getColumnIndex(INFOS_APP_VERSION)));
cursor.close();
return (infos);
}
}
return (null);
}
public int getInfosCount() {
String countQuery = "SELECT * FROM " + TABLE_INFOS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
cursor.close();
return cnt;
}
}
the java activity where i'm using the function getInfo:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
SQLHelper sqlHelper = new SQLHelper(this);
InfosModel infos = sqlHelper.getInfos();
Log.i("infos", infos.getMail());
}
The problem comes from the getInfos() function i believe.
My cursor isn't null but the function moveToFirst() fails but i don't know why and i don't know what to do to fix this since i've never had that problem before and my database was working well ..
thank you for the help :)
bottus.
edit : i tryed to see how many row i'm having in the infos table, there is zero row and that's why movetofirst() fail i believe but the behavior is exactly the same if i had a row in the table ..
Per the Android documents:
This method will return false if the cursor is empty.
So you must not have any data there, and you should manage appropriately. Check for typos, etc.
I don't see any mistakes with my code, I browsed questions with the same issue but I couldn't solve
it, I tried upgrading the sql version from 1 to 2, but still same error.
Is there anything wrong with my code?
public String place_ID = "id";
public String place_LAT = "LAT";
public String place_LNG = "LNG";
public String place_name = "name";
public String place_number = "number";
public String place_city = "city";
public String place_country = "country";
public String place_street = "street";
public String place_smallDesc = "smallDesc";
public String place_bigDesc = "bigDesc";
public String place_site = "site";
public String place_category = "category";
public String place_imageName ="image";
public String place_rank = "rank";
final static String PASS = "testPass";
final static String db_Name = "placesManager";
final static String DBTABLE_NAME_STRING = "places";
public DatabaseHelper(Context context) {
super(context, db_Name,null,2);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
String createDBQuery = "CREATE TABLE " + DBTABLE_NAME_STRING + "("
+ place_ID + " INTEGER PRIMARY KEY," + place_LAT + " REAL,"
+ place_name + " TEXT," + place_number + " TEXT,"
+ place_city + " TEXT," + place_country + " TEXT,"
+ place_street + " TEXT" + place_smallDesc + " TEXT,"
+ place_site + " TEXT" + place_category + " TEXT,"
+ place_imageName + " TEXT"+ place_rank + " INTEGER " + ")";
db.execSQL(createDBQuery);
}
public void AddPlaces(ArrayList<Place> placesList)
{
SQLiteDatabase db = this.getWritableDatabase(PASS);
ContentValues cValues = new ContentValues();
Log.d("MyTag","inserting");
for(int i = 0;i<placesList.size();i++)
{
cValues.put(place_ID, placesList.get(i).ID);
cValues.put(place_LAT, placesList.get(i).LAT);
cValues.put(place_LNG, placesList.get(i).LNG);
cValues.put(place_name, placesList.get(i).name);
cValues.put(place_number, placesList.get(i).number);
cValues.put(place_city, placesList.get(i).city);
cValues.put(place_ID, placesList.get(i).ID);
cValues.put(place_street, placesList.get(i).street);
cValues.put(place_smallDesc, placesList.get(i).smallDesc);
cValues.put(place_site, placesList.get(i).site);
cValues.put(place_category, placesList.get(i).category);
cValues.put(place_imageName, placesList.get(i).imageName);
cValues.put(place_rank, placesList.get(i).rating);
db.insert(DBTABLE_NAME_STRING, null, cValues);
}
Log.d("MyTag","done inserting");
db.close();
}
Inside onCreate you have:
+ place_street + " TEXT" + place_smallDesc + " TEXT,"
should be like below(missed colon), due to colon miss, both merged and table will have completely different column name than you are assuming.
+ place_street + " TEXT, " + place_smallDesc + " TEXT,"