SQLite Manager not correctly generating query - java

This is my query and associative variables:
public static final String DATABASE_NAME = "HDL_db";
public static final String TABLE_NAME = "HDL_table";
public static final String COL_3 = "Arrival_Time";
public static final int database_version = 1;
public String CREATE_QUERY = "CREATE TABLE " + TABLE_NAME + " (" +
COL_3 + " DATE)";
This is the resultant query format in SQLite Manager (Mozilla Firefox add-on):
CREATE TABLE HDL_table(Arrival TimeDATE)
should be:
CREATE TABLE HDL_table (Arrival_Time DATE) [based on spaces I have added in original query]
and so leaves me with the following table structure in SQLite Manager:
SQLite Manager Table Structure
I want to have the 'Name' column set to 'Arrival_Time' and the 'Type' set to 'DATE' .. because it will be a date that workers arrive on site.
Personally, I think the spaces in the query are the problem, however when I add in the spaces in the original query, export and run the database file through SQLite Manager and check the resultant table structure .. it is in the wrong format.
Any help with this would be great, thanks.

In your column's name must not contain spaces. SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
"CREATE TABLE IF NOT EXISTS HDL_table (_id integer primary key, arrival_time text)"
Source: https://www.sqlite.org/datatype3.html

Related

Creating a sequence on an existing table - ORA-00940: invalid ALTER command

I created a sequence and I want a table to make use of it. The creation of the sequence works fine. However, I when I try to alter the table in order to make use of the sequence, I get this error (in personInformationSequenceAlterTest):
ORA-00940: invalid ALTER command
Please note I need to use Java (Eclipse IDE).
String personInformationSequenceTest =
"CREATE SEQUENCE seq_person "
+ "start with 1 "
+ "increment by 1 "
+ "NOCACHE "
+ "NOCYCLE ";
String personInformationSequenceAlterTest =
"alter table personInformationTest "
+ "alter column personId "
+ "set default nextval('seq_person')";
String personInformationSequenceOwnedTest =
"alter sequence seq_person owned by personInformationTest.personId";
Your alter statement has syntax problem.
Try this (assuming datatype is int for that column. Change accordingly):
alter table personInformationTest modify (personId int default seq_person.nextval);
This will only work in Oracle 12c and up.
For 11g or lower, you can use triggers. If you don't want to use triggers, you can explicitly use seq_person.nextval in your inserts.
insert into personInformationTest (personId, . . .)
values (seq_person.nextval, . . .)
Check by Changing
String personInformationSequenceAlterTest =
"alter table personInformationTest "
+ "alter column personId "
+ "set default nextval('seq_person')";
to
String personInformationSequenceAlterTest =
"alter table personInformationTest "
+ "modify column personId "
+ "set default nextval('seq_person')";
In Oracle and MySql we use "Modify" for altering an existing column . In SQL Server / MS Access , "Alter" is used .

transfer data from text table to normal table hsqldb java

I am trying to transfer data from a text table to a normal table, where the data is taken originally from a txt file.
I am using hsqldb
This is what I did. I have no error or exception , but both the tables are empty.
String sqlkeywordcreate=new String ("CREATE TABLE keywordsTable " + " (k_id INTEGER IDENTITY not NULL PRIMARY KEY, keywords varchar(20))");
String sqlkeywordcreate1=new String ("CREATE TEXT TABLE tempKeywordsTable " + " (key varchar(20))");
stmt1.executeUpdate(sqlkeywordcreate);
stmt1.executeUpdate(sqlkeywordcreate1);
int numOfFields=di.getAllTerms();
String setTempKeywordsTable= new String ("set table "+"tempKeywordsTable"+ " source 'keywords.txt'");
//System.out.print(setTempKeywordsTable);
stmt1.executeUpdate( setTempKeywordsTable);
String insertkey= new String("INSERT INTO keywordsTable "+"(keywords)"+ " select key from tempKeywordsTable");
stmt1.executeUpdate(insertkey);
String dropTempKey= new String("drop table tempKeywordsTable");
//stmt1.executeUpdate(dropTempKey);
String sqlcreate=new String("CREATE TABLE "+ tableName +" (id INTEGER IDENTITY not NULL PRIMARY KEY)");
String sqlselect=new String("select k_id from keywordsTable");
Please guide me and give me ideas to solve this issue.
Thanks
The code looks correct. You should add a test count after the line with "set table tempKeywordsTable source " to make sure the data is linked properly to the TEXT table. If there is data, the rest will work.
A possible cause of your code not working is the path of the keywords.txt file. This file should be in the same directory as the rest of the database files.

Java-Sqlite Truncate all Database tables

How to Truncate All Tables in Sqlite Database using Java?
I know i can do this
{
...
String table_name1 = "Delete From table_name1";
String table_name2 = "Delete From table_name2";
String table_name3 = "Delete From table_name3";
String table_name4 = "Delete From table_name4";
String table_name5 = "Delete From table_name5";
stmt.executeUpdate(table_name1);
...
}
I can execute all these Strings to do the task but it increases the Lines of Code.
Is there a way to Truncate all tables in a Database with a single command, without separately typing there names?
There's no TRUNCATE in sqlite. You can use DELETE FROM <tablename> to delete all table contents. You'll need to do that separately for each table.
However, if you're interested in removing all data, consider just deleting the database file and creating a new one.
SQLite do not have TRUNCATE TABLE command in SQLite but you can use SQLite DELETE command to delete complete data from an existing table, though it is recommended to use DROP TABLE command to drop complete table and re-create it once again.
Try
{
...
String table_name1 = "DELETE FROM table_name1";
String table_name2 = "DELETE FROM table_name2";
String table_name3 = "DELETE FROM table_name3";
String table_name4 = "DELETE FROM table_name4";
String table_name5 = "DELETE FROM table_name5";
stmt.executeUpdate(table_name1);
...
}

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.

keep column name variable in Java INSERT INTO command with PreparedStatement?

I have the following problem:
I have two tables in one data base which consist of the same columns besides the name of the last column. I want to write data into them using Java.
I want to use the same preparedStatement for both tables, where I check with an if-command whether it is table1 or table2. table2 has amount10 as the name for the last column, table1 has amount20 for it. This number is stored in a variable within my code.
Below you can see a (simplified) example and how I tried to let the column name variable but it doesn't work. Is there any way to fix this without copying the whole statement and manually changing the number variable?
String insertData = "INSERT INTO `database`.`"+table+"`
(`person_id`,`Date`,`amount`+"number") VALUES "+
"(?,?,?) ON DUPLICATE KEY UPDATE " +
"`person_id` = ? , " +
"`Date` = ? , " +
"`amount`+"number" = ? ; ";
PreparedStatement insertDataStmt;
This will not work since variables number and table are not going to be magically injected into your insertData string while you are changing them.
I'd to a method prepareInsertstatement(String table, String number) that would return correct PreparedStatement:
public void prepareInsertStatement(Connection conn, Strint table, String number) {
String insertData = "INSERT INTO `database`.`"+table+"`
(`person_id`,`Date`,`amount+"number"') VALUES "+
"(?,?,?) ON DUPLICATE KEY UPDATE " +
"`person_id` = ? , " +
"`Date` = ? , " +
"`amount+"number"' = ? ; ";
PreparedStatement insertDataStmt = conn.prepareStatement(insertData);
return insertDataStmt;
}
Just remember to close the PreparesStatement when you don't need it any more.
I suppose that reason for that is invalid syntax. When you concatenate string for last column name you use code 'amount' + number. If your number value is 20, than concat result will be
'amount'20 that cause invalid syntax exception. Just move one extra ' after number.
"'amount" + number + "'"
Note: log, or just error that appears during this statement execution would be very useful to find right answer for your question.

Categories

Resources