java.sql.SQLException: ORA-00936: missing expression - java

Below I am creating table.
public static final String CREATE_SQL = "CREATE TABLE " +DATABASE_TABLE +
"(ID number(10,0), " +
" CGUID VARCHAR(255), " +
" PGUID VARCHAR(255), " +
" SGUID VARCHAR(255), " +
" USERID VARCHAR(255), " +
" ULOC VARCHAR(255), " +
" SLOC VARCHAR(255), " +
" PLOC VARCHAR(255), " +
" ALOC VARCHAR(255), " +
" SITEID VARCHAR(255), " +
" ATTRIBUTEID VARCHAR(255), " +
" ATTRIBUTEVALUE VARCHAR(255), " +
" PRIMARY KEY ( ID ))";
This is the below UPSERT_SQL query when I am trying to update my database table I am always getting- java.sql.SQLException: ORA-00936: missing expression
. I checked my SQL, I am not able to find where the expression is missing. Is something wrong with the below SQL?
public static final String UPSERT_SQL = "MERGE INTO " +DATABASE_TABLE+ " USING ( SELECT ? AS ID, " + // We will maybe add this record
" ? AS CGUID, " +
" ? AS PGUID, " +
" ? AS SGUID, "+
" ? AS USERID, "+
" ? AS ULOC, "+
" ? AS SLOC, "+
" ? AS PLOC, "+
" ? AS ALOC, "+
" ? AS SITEID, "+
" ? AS ATTRIBUTEID, "+
" ? AS ATTRIBUTEVALUE, "+
" FROM dual ) maybe "+
" ON (maybe.ID = "+DATABASE_TABLE+".ID) "+
" WHEN MATCHED THEN "+
// We only need update the fields that might have changed
" UPDATE SET " +DATABASE_TABLE+ ".ULOC = maybe.ULOC, " +DATABASE_TABLE+ ".SLOC = maybe.SLOC, " +DATABASE_TABLE+ ".PLOC = maybe.PLOC, " +DATABASE_TABLE+ ".ALOC = maybe.ALOC "+
" WHEN NOT MATCHED THEN "+
// Insert new record
" INSERT VALUES (maybe.ID, maybe.CGUID, maybe.PGUID, maybe.SGUID, maybe.USERID, maybe.ULOC, maybe.SLOC, maybe.PLOC, maybe.ALOC, maybe.SITEID, maybe.ATTRIBUTEID, maybe.ATTRIBUTEVALUE)";
And from below I am executing that UPSERT_SQL Statement.
LnPDataConstants.PSTMT = LnPDataConstants.DB_CONNECTION.prepareStatement(LnPDataConstants.UPSERT_SQL); // create a statement
LnPDataConstants.PSTMT.setInt(1, (int) ind);
LnPDataConstants.PSTMT.setString(2, LnPDataConstants.CGUID_VALUE);
LnPDataConstants.PSTMT.setString(3, LnPDataConstants.PGUID_VALUE);
LnPDataConstants.PSTMT.setString(4, LnPDataConstants.SGUID_VALUE);
LnPDataConstants.PSTMT.setString(5, LnPDataConstants.UID_VALUE);
LnPDataConstants.PSTMT.setString(6, LnPDataConstants.ULOC_VALUE);
LnPDataConstants.PSTMT.setString(7, LnPDataConstants.SLOC_VALUE);
LnPDataConstants.PSTMT.setString(8, LnPDataConstants.PLOC_VALUE);
LnPDataConstants.PSTMT.setString(9, LnPDataConstants.ALOC_VALUE);
LnPDataConstants.PSTMT.setString(10, LnPDataConstants.SITEID_VALUE);
LnPDataConstants.PSTMT.setString(11, "10200");
LnPDataConstants.PSTMT.setString(12, attrValue1.toString().split("=")[1]);
LnPDataConstants.PSTMT.executeUpdate();

Yes, there is something wrong with the SQL, and it is that you wrote a comma before FROM dual. This causes Oracle's SQL parser to complain.

Related

Selecting Items from another table using ID

I have two tables Item and a table which records the items in each order (Junction Table)
ITEM Table
String itemTable = "CREATE TABLE " + ITEM_TABLE + " ("
+ ID_ITEM + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ITEM_NAME + " TEXT,"
+ ITEM_TYPE + " TEXT,"
+ ITEM_PRICE + " TEXT);";
ORDER_ITEM Table
String orderItemTable = "CREATE TABLE " + ORDER_ITEM_TABLE + " ("
+ ID_ORDER_ITEM + " INTEGER,"
+ ID_ITEM_ORDER + " INTEGER,"
+ " FOREIGN KEY ("+ID_ORDER_ITEM+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"), "
+ " FOREIGN KEY ("+ID_ITEM_ORDER+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"));";
Data in the ORDER_ITEM table each Item id refers to a specific item in the item table this is what I am trying to refer to and use to pull data from the DB.
At the moment I can pull the Item id from this table but not the actual Item using the Id. Here idOrder is passed when the order is selected
String selctAllEmployeesOrdersItems = "SELECT * FROM " + ORDER_ITEM_TABLE + " WHERE " + ID_ORDER_ITEM + " = " + idOrder;
One way of achieving what I want is by storing the Item ID's in a ArrayList and iterating over this to select all of the Items but I know there is a better way.
Some research I have seen has suggested that I join the tables referencing the ITEM ID but I am not sure of the correct syntax. The dot notation does not work with this query.
String selctAllEmployeesOrdersItems = "SELECT * FROM " + ORDER_ITEM_TABLE + " WHERE " + ID_ORDER_ITEM + " = " + idOrder
+ " JOIN " + ITEM_TABLE + " ON " + ORDER_ITEM_TABLE+"."+ID_ITEM_ORDER + " = " + ITEM_TABLE+"."+ID_ITEM;
The error message being show is
Caused by: android.database.sqlite.SQLiteException: near "JOIN":
syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM
ORDER_ITEM_TABLE WHERE ID_Order = 1 JOIN ITEM_TABLE ON
ORDER_ITEM_TABLE.ID_Item = ITEM_TABLE.ID
The syntax of your SQL statement is wrong.
The WHERE clause must be written after the join:
String selctAllEmployeesOrdersItems =
"SELECT * FROM " + ORDER_ITEM_TABLE + " AS o " +
"INNER JOIN " + ITEM_TABLE + " AS i ON o." + ID_ITEM_ORDER + " = i." + ID_ITEM + " " +
"WHERE o." + ID_ORDER_ITEM + " = " + idOrder;
Note the use of aliases o and i for the 2 tables that shortens significantly the code.
Also, the definition of the table ORDER_ITEM:
String orderItemTable = "CREATE TABLE " + ORDER_ITEM_TABLE + " ("
+ ID_ORDER_ITEM + " INTEGER,"
+ ID_ITEM_ORDER + " INTEGER,"
+ " FOREIGN KEY ("+ID_ORDER_ITEM+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"), "
+ " FOREIGN KEY ("+ID_ITEM_ORDER+") REFERENCES "+ EMP_TABLE +"("+ ID_EMP +"));";
does not seem correct.
What is the table EMP_TABLE?
Why do both columns ID_ORDER_ITEM and ID_ITEM_ORDER reference the same column?
This does not make sense.
I believe you have the clauses in the wrong order, this should be:
select ... from ... join ... on ... where ...
So in this case:
SELECT * FROM
ORDER_ITEM_TABLE JOIN ITEM_TABLE
ON ORDER_ITEM_TABLE.ID_Item = ITEM_TABLE.ID
WHERE ORDER_ITEM_TABLE.ID_Order=1;
The where clause goes after the join. I would also recommend using table aliases to shorten the query and make it more readable. Finally, you probably want to select columns from the items table only, not from the junction table (which you are filtering on already):
SELECT i.*
FROM ORDER_ITEM_TABLE oi
JOIN ITEM_TABLE i ON oi.ID_Item = i.ID
WHERE oi.ID_Order = 1

Syntax to create unique composite column - Android SQLiteOpenHelper

I have this onCreate method in my SQLiteOpenHelper
class, and I would like to add a unique constraint on these two columns (composite unique columns):
SongContract.SongEntry.COLUMN_TITLE
SongContract.SongEntry.COLUMN_RELEASEDATE
But I am getting an error:
Cannot resolve method UNIQUE
Here is my code:
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_SONG_TABLE = "CREATE TABLE " + SongContract.SongEntry.TABLE_SONG + " (" +
SongContract.SongEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
SongContract.SongEntry.COLUMN_TITLE + " TEXT NOT NULL, " +
SongContract.SongEntry.COLUMN_RELEASEDATE + " INTEGER, " +
UNIQUE(SongContract.SongEntry.COLUMN_TITLE, SongContract.SongEntry.COLUMN_RELEASEDATE) +
SongContract.SongEntry.COLUMN_RATING + " TEXT);";
db.execSQL(SQL_CREATE_SONG_TABLE);
}
What is the correct syntax to achieve my goal?
I found the corrrect syntax after playing around sqllite:
final String SQL_CREATE_SONG_TABLE = "CREATE TABLE " + SongContract.SongEntry.TABLE_SONG + " (" +
SongContract.SongEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
SongContract.SongEntry.COLUMN_TITLE + " TEXT NOT NULL, " +
SongContract.SongEntry.COLUMN_RELEASEDATE + " INTEGER NOT NULL, " +
SongContract.SongEntry.COLUMN_RATING + " TEXT, " + "UNIQUE" + "(" +
SongContract.SongEntry.COLUMN_TITLE + "," + SongContract.SongEntry.COLUMN_RELEASEDATE + ") " + ");";

Android: SQLite says a column doesn't exist

I am trying to get all the values in a table that have column _parentbook set to a certain value. When I try to retrieve the entries I get the error shown below.
E/SQLiteLog: (1) table recipes has no column named _parentbook
E/SQLiteDatabase: Error inserting _parentbook=Test _recipemethod=Stir in pot for 20 mins _recipeingredients=No bugs, freedom _recipedescription=Test recipe _recipename=Recipe 1 in Test _recipenotes=Do on Android Studio
The error refers to the method below that I use to add a recipe to the database
public void addRecipe(Recipe recipe) {
ContentValues values = new ContentValues();
values.put(COLUMN_RECIPE_NAME, recipe.getRecipeTitle());
values.put(COLUMN_RECIPE_DESCRIPTION, recipe.getRecipeDescription());
values.put(COLUMN_RECIPE_INGREDIENTS, recipe.getIngredients());
values.put(COLUMN_RECIPE_METHOD, recipe.getMethod());
values.put(COLUMN_RECIPE_NOTES, recipe.getNotes());
//values.put(COLUMN_IMAGE_ID, recipe.getImageId());
values.put(COLUMN_PARENT_BOOK, recipe.getParentBook());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_RECIPES, null, values);
db.close();
}
Code used to initialise TABLE_RECIPES:
String CREATE_TABLE_RECIPES = "CREATE TABLE IF NOT EXISTS " + TABLE_RECIPES + " (" +
COLUMN_ID_2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_RECIPE_NAME + " TEXT, " +
COLUMN_RECIPE_DESCRIPTION + " TEXT, " +
COLUMN_RECIPE_INGREDIENTS + " TEXT, " +
COLUMN_RECIPE_METHOD + " TEXT, " +
COLUMN_RECIPE_NOTES + " TEXT, " +
COLUMN_IMAGE_ID + " INTEGER " +
COLUMN_PARENT_BOOK + " TEXT" +
");";
#Override
public void onCreate(SQLiteDatabase db) {
Log.e(TAG, "OnCreate() called");
db.execSQL(CREATE_TABLE_RECIPES);
}
Method for getting the recipes from the table:
List<Recipe> recipes;
public List<Recipe> getRecipes(String bookName) {
recipes = new ArrayList<>();
SQLiteDatabase db = getWritableDatabase();
//String query = "SELECT "+ COLUMN_PARENT_BOOK +" FROM " + TABLE_RECIPES + " WHERE " + COLUMN_PARENT_BOOK + "=" + bookName;
String query = "SELECT * FROM " + TABLE_RECIPES;// + " WHERE 1";
// Cursor going to point to a location in the results
Cursor c = db.rawQuery(query, null);
// Move it to the first row of your results
c.moveToFirst();
if (c.moveToFirst()) {
do {
if (c.getString(c.getColumnIndex(COLUMN_RECIPE_NAME)) != null) {
recipes.add(new Recipe(
c.getString(c.getColumnIndex(COLUMN_RECIPE_NAME)),
c.getString(c.getColumnIndex(COLUMN_RECIPE_DESCRIPTION)),
c.getString(c.getColumnIndex(COLUMN_RECIPE_INGREDIENTS)),
c.getString(c.getColumnIndex(COLUMN_RECIPE_METHOD)),
c.getString(c.getColumnIndex(COLUMN_RECIPE_NOTES)),
// Add image here if required
c.getString(c.getColumnIndex(COLUMN_PARENT_BOOK))
));
}
} while (c.moveToNext());
}
db.close();
//c.close();
return recipes;
}
I have tried upgrading the database version and looking at other similar questions on StackOverflow, neither helped.
Thanks.
You forgot to put comma(,) in create table statement.
Instead of
String CREATE_TABLE_RECIPES = "CREATE TABLE IF NOT EXISTS " + TABLE_RECIPES + " (" +
COLUMN_ID_2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_RECIPE_NAME + " TEXT, " +
COLUMN_RECIPE_DESCRIPTION + " TEXT, " +
COLUMN_RECIPE_INGREDIENTS + " TEXT, " +
COLUMN_RECIPE_METHOD + " TEXT, " +
COLUMN_RECIPE_NOTES + " TEXT, " +
COLUMN_IMAGE_ID + " INTEGER " +
COLUMN_PARENT_BOOK + " TEXT" +
");";
It should be
String CREATE_TABLE_RECIPES = "CREATE TABLE IF NOT EXISTS " + TABLE_RECIPES + " (" +
COLUMN_ID_2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_RECIPE_NAME + " TEXT, " +
COLUMN_RECIPE_DESCRIPTION + " TEXT, " +
COLUMN_RECIPE_INGREDIENTS + " TEXT, " +
COLUMN_RECIPE_METHOD + " TEXT, " +
COLUMN_RECIPE_NOTES + " TEXT, " +
COLUMN_IMAGE_ID + " INTEGER, " + // Here you forgot to put comma(,) in this line
COLUMN_PARENT_BOOK + " TEXT" +
");";

java.sql.SQLException: ORA-06550

When I am trying to execute this sql statement, I am getting exception as-
java.sql.SQLException: ORA-06550: line 1, column 429:
PLS-00103: Encountered the symbol "/" The symbol "/" was ignored.
This is the below sql String that I am executing- Is there anything wrong with this sql? It will check whether table is there or not, if it is there then it will not create a table and if it is not there, then it will create a table.
public static final String CREATE1 = "DECLARE " +
"t_count INTEGER; " +
"v_sql VARCHAR2(1000) := 'create table " +DATABASE_TABLE + " ( " +
"(ID number(10,0), " +
"CGUID VARCHAR(255), " +
"PGUID VARCHAR(255), " +
"SGUID VARCHAR(255), " +
"USERID VARCHAR(255), " +
"ULOC VARCHAR(255), " +
"SLOC VARCHAR(255), " +
"PLOC VARCHAR(255), " +
"ALOC VARCHAR(255), " +
"SITEID VARCHAR(255), " +
"PRIMARY KEY ( ID ))'; " +
"BEGIN " +
"SELECT COUNT(*) " +
"INTO t_count " +
"FROM user_tables " +
"WHERE table_name = '" +DATABASE_TABLE + "'; " +
"IF t_count = 0 THEN " +
"EXECUTE IMMEDIATE v_sql; " +
"END IF; " +
"END; ";
It is getting printed on the console as-
DECLARE t_count INTEGER; v_sql VARCHAR2(1000) := 'create table LnPData((ID number(10,0), CGUID VARCHAR(255), PGUID VARCHAR(255), SGUID VARCHAR(255), USERID VARCHAR(255), ULOC VARCHAR(255), SLOC VARCHAR(255), PLOC VARCHAR(255), ALOC VARCHAR(255), SITEID VARCHAR(255), PRIMARY KEY ( ID ))'; BEGIN SELECT COUNT(*) INTO t_count FROM user_tables WHERE table_name = 'LnPData'; IF t_count = 0 THEN EXECUTE IMMEDIATE v_sql; END IF; END;
Remove ';' after your END IF statement and run it again.

Inserting into database

I have written a query to insert values into a database in Android:
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL," + " );");
However, when I execute it, an error is thrown. Can anyone point out the error in it?
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" INTEGER NOT NULL," + KEY_LONGITUDE + " INTEGER NOT NULL" + " );");
Provide a Datatype to KEY_LATITUDE and to KEY_LONGITUDE.
And also you have kept a (,) at last which was not needed...
the correct code is:
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL," + KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL," + " )");
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL);");

Categories

Resources