Synax Error When Trying To Create SQLite Database - java

I am receiving this error when trying to create my database Failure 1 (near "CREATE_TABLE": syntax error) on 0x367f80 when preparing 'CREATE_TABLE cattletypes (_id INTEGER PRIMARY KEY AUTOINCREMENT , cattle_type TEXT);'.
The code I use is
db.execSQL("CREATE_TABLE " + CattleType.CATTLETYPE_TABLE_NAME + " ("
+ CattleType._ID + " INTEGER PRIMARY KEY AUTOINCREMENT , "
+ CattleType.CATTLE_TYPE_NAME + " TEXT"
+ ");");
I've been reading it and rereading it and can't for the life of me figure out where my error is -
my class code is
public static final class CattleType implements BaseColumns{
private CattleType() {}
public static final String CATTLETYPE_TABLE_NAME = "cattletypes";
public static final String CATTLE_TYPE_NAME = "cattle_type";
public static final String DEFAULT_SORT_ORDER = "cattle_type ASC";
}
What am I missing?
Thanks

There is a '_' between CREATE and TABLE. Remove that and it should work.

Remove the _ between CREATE and TABLE.

As I mentioned in my comment, an extraneous underscore has found its way between CREATE and TABLE.

Related

Android SQLite Exception: no such column

I added a date field to my database, but I got an error which sais "No such column named date".
Can anyone point out where is my mistake?
private static final String UID = "id";
private static final String NAME = "Bmi";
private static final String _STATUS = "status";
private static final String WEIGHT = "weight";
private static final String DATE = "curDate";
db.execSQL("CREATE TABLE " + TABLE_NAME + " ( " + UID + " integer primary key autoincrement," +
"" + NAME + " VARCHAR(50), " +
"" + _STATUS + " VARCHAR(255)," +
"" + WEIGHT + " VARCHAR(255)," +
"" + DATE + "TEXT);");
You're missing a space here
"" + DATE + "TEXT);");
It has to be
"" + DATE + " TEXT);");
Note: uninstall and reinstall your app, to get the database re-created on next launch.
First of all it is a bad habit to use name of SQL inbuilt function as a table name correct your 'DATE' name to something else and there is a typo on the same line of DATE rename table and put the space before text like this.
+ CUR_DATE + " TEXT") (CUR_DATE is suggested name).

SQLite One-to-Many Relationship Set/Get Foreign Key

I am building an android app project with SQLite DB.
I got stuck on One-To-Many RelationShip.
This is One
private static final String createTableOrders =
"CREATE TABLE " + TABLE_ORDER + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
...
+ KEY_COLUMN_FORMATS + " INTEGER REFERENCES " + TABLE_FORMATS + "(" + KEY_ID + ")"
+ ");";
This is Many
private static final String createTableFormats =
"CREATE TABLE " + TABLE_FORMATS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
...
+ ");";
My problems is with set/get methods.
Lets say that I would need to get list of ids of all Formats that are in one Order.
I guess same thing goes for set method.
I tried to find this kind of question on SO but most of the question were just for SQL part.
P.S. Spent 3 hours trying to make it by myself but the only thing I got to was using GSON to code list of ids into String but it was just a dead-end.
EDIT: I need to get that information inside the code.
P.S.S I have been doing this for 18 hours so sorry if this is uber-stupid question.
A one-to-many relationship requires the foreign key column in the "many" table:
CREATE TABLE Orders (
ID PRIMARY KEY
);
CREATE TABLE Formats (
ID PRIMARY KEY,
OrderID REFERENCES Orders(ID)
);
To get all formats belonging to an order, you just look up rows with the ID of that order:
SELECT * FROM Formats WHERE OrderID = ?
In Jave:
Cursor cursor = db.query(TABLE_FORMATS,
new String[] { whatever columns you need },
"OrderID = " + orderID,
null, null, null, null, null);
while (cursor.moveToNext()) {
// read one order from the cursor
}
cursor.close();

handling table in SQLITE (android)

I want to manage (create/delete/update) a table with 18 columns. Normally, for creating table, I use below codes. Is there any smarter way, like putting the column names in an array etc.? How people handle large tables?
Thanks for your help, as always.
private static final String COL1 = "col1";
private static final String COL2 = "col2";
private static final String COL3 = "col3";
........
........
private static final String COL18 = "col18";
public dbhandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COL1 + " INTEGER PRIMARY KEY," + COL2 + " TEXT,"
+ COL3 + " TEXT," + .............................+ COL18 + " TEXT")";
db.execSQL(CREATE_TABLE);
}
A smarter way doing so is using db4o (database 4 objects). While creating a database like this has the following advantages:
It is purely based on object database.
No such mapping of tables like in sqlite.
Saves time and volume of code.
Reuse objects by saving and retrieving them as many times as you want.
Benefit from Native Queries.
For more info refer to: http://www.sohailaziz.com/2012/09/using-database-for-objects-db4o-in.html
You can make tuples (column name and type), and store these in an Array. In the onCreate you can loop over it and add it to the CREATE_TABLE string.
But unless you are going to change the columns a lot, a simple copy and paste of lines is more than enough.
If you are use the large table the best way is use the SQLiteManager browser. Remaining all operation do in the normal .java file.If you are use 2 or more table use the SQLiteManager plug-in.

Why use the Constants interface for SQLite to hold field names?

In the SQLite chapter of "Hello, Android", an interface Constants is created to hold some constant strings: the table name and field names.
Why? How is it more useful to use TITLE than "title" directly? Is there something else I'm missing?
And by the way: how should it be organized if I need two tables? Should I make FirstTableConstants and SecondTableConstants interfaces?
If you're going to use a string more than once, its best to create a constant for it.
It prevents creation of the string more than once.
Object allocation isn't free so you will see some performance gain.
Consider the case where you create the string in a loop.
for(int i=0;i<n;++i){
System.out.println("title");
}
vs using a constant:
for(int i=0;i<n;++i){
System.out.println(TITLE);
}
If it took 1ms to allocate the string "title", your run time for the first loop code is N X 1ms.
Also building SQL statements with constants will be quicker than repeatedly creating new strings for the statement keywords for types (text, integer,) modifiers (unique, not null), etc.
String TABLE_SCHEMA = "(" +
ID + " integer primary key autoincrement, " +
COL_FOO1 + " text, " +
COL_FOO2 + " text, " +
COL_FOO3 + " text, " + ...
If you make " text, " a constant you aren't creating that string over and over again, so your TABLE_SCHEMA string is created quicker.
String TABLE_SCHEMA = "(" +
ID + " integer primary key autoincrement, " +
COL_FOO1 + TYPE_TEXT +
COL_FOO2 + TYPE_TEXT +
COL_FOO3 + TYPE_TEXT + ...
If you're using a class, you should probably have them as public static final String TITLE = "Some Title" field. It's better to use it this way so that you won't make an error while typing it into a command. You'll only have to type the variable name.

Android SQLite3 problem enforcing primary key

I have a listview with a custom adapter that uses an sql cursor. When I add items to my listview, the item will be added to my sqlite3 database and the listview will refresh.
My one problem is that I am able to add duplicate items, and I do not want my application to allow duplicates to be added.
I have added a primary key to my database (on itemNumber), however the database does not seem to be enforcing this. Here is how I create my database:
private static final String DB_CREATE_MASTER = "CREATE TABLE "
+ "MyTable"
+ " (_id INTEGER, itemNumber TEXT,"
+ "itemPlace TEXT," + "itemTimeTEXT,"
+ "itemCode INTEGER,"
+ "dbdatestamp TEXT" + "PRIMARY KEY(itemNumber)" +");";
Does anyone know why I am able to add more than 1 duplicate itemNumber to my listview?
Thanks!
EDIT:
private static final String DB_CREATE_MASTER = "CREATE TABLE "
+ "MyTable"
+ " (_id INTEGER PRIMARY KEY, itemNumber TEXT,"
+ "itemPlace TEXT," + "itemTimeTEXT,"
+ "itemCode INTEGER,"
+ "dbdatestamp TEXT" + "UNIQUE(itemNumber)" +");";
EDIT:
This is how I add the itemNumber to the database:
values.put("itemNumber", myClass.itemNumber);
values.put("itemName", myClass.itemName);
values.put("itemTime", myClass.itemTime);
values.put("dbdatestamp", "03/01/1960 08:55");
this.db.insert(MY_TABLE, null, values);
Your primary key consists of 2 fields: _id and itemNumber. That means that a duplicate itemNumber is not enough to enforce the constraint. A duplicate would be an item with the same _id AND itemNumber. You could change the CREATE statement like this
PRIMARY KEY(_id), UNIQUE(itemNumber)
The UNIQUE constraint wouldn't allow duplicate item numbers.

Categories

Resources