From Javafile to SQL query - Minecraft plugin - java

as i am working on a minecraft server some plugin does not make a certain table itself.
For who is interested: https://www.spigotmc.org/resources/craftconomy.2395/
But in the github files i found the java script where it does generate the needed table.
public final String createTableMySQL = "CREATE TABLE IF NOT EXISTS " + getPrefix() + TABLE_NAME + " (" +
" `" + BALANCE_FIELD + "` double DEFAULT NULL," +
" `" + WORLD_NAME_FIELD + "` varchar(255)," +
" `username_id` int(11)," +
" `" + CURRENCY_FIELD + "` varchar(50)," +
" PRIMARY KEY (" + WORLD_NAME_FIELD + ", username_id, currency_id)," +
" CONSTRAINT `"+getPrefix()+"fk_balance_account`" +
" FOREIGN KEY (username_id)" +
" REFERENCES " + getPrefix() + AccountTable.TABLE_NAME + "(id) ON UPDATE CASCADE ON DELETE CASCADE," +
" CONSTRAINT `"+getPrefix()+"fk_balance_currency`" +
" FOREIGN KEY (" + CURRENCY_FIELD + ")" +
" REFERENCES " + getPrefix() + CurrencyTable.TABLE_NAME +"(name) ON UPDATE CASCADE ON DELETE CASCADE" +
") ENGINE=InnoDB CHARSET=utf8;";
Could someone help me to make a SQL query out of this, which just can be dropped into PHPMyadmin?
Thanks already for reading.

I already solved it myself, no reason for reaction anymore.

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 + ") " + ");";

"No such column" when running update on SQLite

I'm trying to update a row in my database. Here is the code I am trying to use to update:
public void addFBComments(int id){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS+"="+"HELLO" + " WHERE " + COL_FB_ID+"="+id;
db.execSQL(query);
}
When I run the command, it throws this error:
08-18 15:42:10.145: E/AndroidRuntime(10493): FATAL EXCEPTION: Thread-922
08-18 15:42:10.145: E/AndroidRuntime(10493): android.database.sqlite.SQLiteException: no such column: HELLO (code 1): , while compiling: UPDATE fb_feed SET fb_comments= HELLO WHERE _id=3
Here is the TABLE_FB_FEED:
private static final String TABLE_FB_FEED_CREATE = "create table " + TABLE_FB_FEED +
" ("
+ COL_FB_ID + " integer primary key autoincrement not null,"
+ COL_FB_MESSAGE + " text,"
+ COL_FB_CAPTION + " text,"
+ COL_FB_POSTER_ID + " text,"
+ COL_FB_POST_ID + " text,"
+ COL_FB_LIKES + " text,"
+ COL_FB_POSTER_NAME + " text,"
+ COL_FB_COMMENTS + " text," // this is the column i want to update
+ COL_FB_LIKES_NUM + " text,"
+ COL_FB_TIMESTAMP + " text,"
+ COL_FB_PROFILE_PICTURE_URI + " text,"
+ COL_FB_PICTURE_URI + " text,"
+ COL_FB_NAME + " text,"
+ COL_FB_PREVIOUS_PAGE_URI + " text,"
+ COL_FB_NEXT_PAGE_URI + " text,"
+ COL_FB_POST_TYPE + " text,"
+ COL_FB_LINK_NAME + " text,"
+ COL_FB_COMMENTS_NUM + " text,"
+ COL_FB_STORY + " text,"
+ COL_FB_DESCRIPTION + " text,"
+ COL_FB_COMMENTS_BEFORE + " text,"
+ COL_FB_COMMENTS_AFTER + " text,"
+ COL_FB_LIKES_PROFILE_PIC + " text,"
+ COL_FB_COMMENTS_PROFILE_PIC + " text);";
Why is it throwing the query? I thought I was doing everything correctly.
in your code you need to add single quotes:
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS
+"="+"'HELLO'" + " WHERE " + COL_FB_ID+"="+id
or
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS
+"='HELLO' WHERE " + COL_FB_ID+"="+id
this is another example
"UPDATE DB_TABLE SET YOUR_COLUMN='newValue' WHERE id=myidValue");
In SQL, string values must be quoted:
String query = "..." + COL_FB_COMMENTS+"= 'HELLO' WHERE ...";
Anyway, formatting problems such as this (which get worse when the string itself contains quotes) can be avoided by using parameters:
String query = "... SET " + COL_FB_COMMENTS+"= ? WHERE " + COL_FB_ID+"="+id;
db.execSQL(query, new Object[] { "HELLO" });
You need to escape your String literal,
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS
+"="+"HELLO" + " WHERE " + COL_FB_ID+"="+id
Could be
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS
+"="+"'HELLO'" + " WHERE " + COL_FB_ID+"="+id
Or, you could use a bind parameter.
something like
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS+"="+"'HELLO'" + " WHERE " + COL_FB_ID+"="+id;
would work

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);");

Android SQLite : constraint failed error code 19

I have execute the following sql:
update record set out_payment=4 where out_payment=4;
the out_payment of record is defined as Integer and reference the Payment(id)
I can ensure the 4 was the one of the ID of the payment table;
but I still got the constrained failed....
07-31 10:20:36.014: ERROR/Database(19085): Error updating out_payment=4 using UPDATE record_table SET out_payment=? WHERE out_payment = 4
07-31 10:20:45.964: ERROR/AndroidRuntime(19085): FATAL EXCEPTION: main
07-31 10:20:45.964: ERROR/AndroidRuntime(19085): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
the code is as following:
values.clear();
values.put(RecordSchema.ID_OUT_PAYMENT, oldid);
selection = RecordSchema.ID_OUT_PAYMENT + " = " + oldid + "";
this.db.update(Table.RECORD, values, selection, null);
the Schema is as following :
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE.RECORD
+ " (" + RecordSchema.ID + " INTEGER PRIMARY KEY"
+ "," + RecordSchema.AMOUNT + " TEXT NOT NULL"
+ "," + RecordSchema.ID_CATEGORY + " INTEGER NOT NULL"
+ "," + RecordSchema.ID_SUBCATEGORY + " INTEGER"
+ "," + RecordSchema.DATE + " DATE NOT NULL"
+ "," + RecordSchema.ID_IN_PAYMENT + " INTEGER"
+ "," + RecordSchema.ID_OUT_PAYMENT + " INTEGER"
+ ",FOREIGN KEY(" + RecordSchema.ID_CATEGORY + ") REFERENCES "
+ IsAiZanTable.CATEGORY + "(" + CategorySchema.ID + ") ON UPDATE CASCADE"
+ ",FOREIGN KEY(" + RecordSchema.ID_SUBCATEGORY + ") REFERENCES "
+ IsAiZanTable.SUBCATEGORY + "(" + SubcategorySchema.ID + ") ON UPDATE CASCADE"
+ ",FOREIGN KEY(" + RecordSchema.ID_IN_PAYMENT + ") REFERENCES "
+ IsAiZanTable.PAYMENT + "(" + PaymentSchema.ID + ") ON UPDATE CASCADE"
+ ",FOREIGN KEY(" + RecordSchema.ID_OUT_PAYMENT + ") REFERENCES "
+ IsAiZanTable.PAYMENT + "(" + PaymentSchema.ID + ") ON UPDATE CASCADE"
+ ");");
db.execSQL("CREATE TABLE IF NOT EXISTS " +Table.PAYMENT
+ " (" + PaymentSchema.ID + " INTEGER PRIMARY KEY"
+ "," + PaymentSchema.KIND + " INTEGER NOT NULL"
+ "," + PaymentSchema.NAME + " TEXT NOT NULL UNIQUE"
+ "," + PaymentSchema.TOTAL + " TEXT NOT NULL"
+ "," + PaymentSchema.HIDDEN + " INTEGER NOT NULL"
+ ");");
Any body can help me to solve this problem?
Actually I want to update the id from 4 to 5
I can ensure both 4 and 5 are the exist IDs of the payment table.
But the same problem occurred, so I update 4 to 4 is the same.
I think if I can solve the problem of 4 to 4 , I should solve the problem of 4 to 5 .
Many thanks for your answer!!
I have found the answer.
One of my team run the following
db.execSQL("PRAGMA foreign_keys = OFF;");
then put the 0 into the ID_OUT_PAYMENT of record table .
The 0 is not the exist id of the PAYMENT table.
So when I update the ID_OUT_PAYMENT it will trigger the constraint failed.
So If I want to make my sql above run successfully.
I need to run the sql too.
db.execSQL("PRAGMA foreign_keys = OFF;");
Thanks very much for every one to reply me!!
Thanks!

Categories

Resources