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.
Related
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 .
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
I am using org.apache.hadoop.hbase library to query hbase.
In h-base I have following:
table name : Employees
in this table I have following fields:
emp_id,name, age, designation, joining_date
how can I find designation of employee if I have his joining_date and name only.
I am using something like:
public static void getOneRecord (Configuration conf, String tableName, String rowKey, String rowKey2) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(rowKey.getBytes());
Result rs = table.get(get);
for(KeyValue kv : rs.raw()){
System.out.print(new String(kv.getRow()) + " " );
System.out.print(new String(kv.getFamily()) + ":" );
System.out.print(new String(kv.getQualifier()) + " " );
System.out.print(kv.getTimestamp() + " " );
System.out.println(new String(kv.getValue()));
}
}
how can I extend it to 2 row keys.
Note: It is guaranteed that only one value will be returned by this pair query.
You want to get a record based on the columns which are not row keys. Please confirm this. If yes, following are the options
Good row key design for performance. If you would be querying more based on a column other than row key, you can consider looking at modifying row key design to append that column as part of row key. Then you can use get or scan with row key range, row filter or FuzzyRowFilter
Secondary index is one option.
Use filters(in your case two SingleColumnValueFilter) and add them as part of scan.here
I am trying to create a text table set its values from a file and then insert these values back into a normal table using hsqldb in a Java code as shown in this code:
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))");
System.out.println(sqlkeywordcreate);
stmt1.executeUpdate(sqlkeywordcreate);
stmt1.executeUpdate(sqlkeywordcreate1);
int numOfFields=di.getAllTerms();
String setTempKeywordsTable= new String ("set table "+ "tempKeywordsTable"+ " source 'E:/Thesis/ThesisWork/outdata/keywords.txt';ignore_first=false;shutdown=true");
stmt1.execute( 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);
But when I run this I got this exception:
java.sql.SQLException: bad TEXT table source file - line number: 0 org.hsqldb.HsqlException: Access is denied: E:/Thesis/ThesisWork/outdata/keywords.txt in statement [set table tempKeywordsTable source 'E:/Thesis/ThesisWork/outdata/keywords.txt']
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source)
at ThesisCode.ReductionTry.main(ReductionTry.java:73)
Exception in thread "main" java.lang.NullPointerException
at ThesisCode.ReductionTry.main(ReductionTry.java:147)
I searched a lot to figure out the problem but I could not find anything.
Please give me some advice.
Watch out for the spaces when concatenating strings, you should write:
String setTempKeywordsTable= new String ("set table "+"tempKeywordsTable"+ " source 'E:/Thesis/ThesisWork/outdata/keywords.
instead of:
String setTempKeywordsTable= new String ("set table"+ "tempKeywordsTable"+ " source 'E:/Thesis/ThesisWork/outdata/keywords.
You're doing the same mistake in String insertkey.
One update from me:
Now we must use relative path. For example: db exist here: "c:\folder1\folder2". And file with data should be in folder2 or in child folders.
How can i manually insert values if not exist...i tried following code but it produce error.How can i insert values if not exist in the table
String sql1 = "CREATE TABLE IF NOT EXISTS admin " +
"(id INTEGER not NULL AUTO_INCREMENT, " +
" user_name VARCHAR(255), " +
" password VARCHAR(255), " +
" isAdmin BOOLEAN NOT NULL DEFAULT '0', " +
" memo VARCHAR(255), " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql1);
String insert="INSERT INTO admin IF NOT EXISTS(id,user_name,password,isAdmin,memo)VALUES(1,'admin','admin',1,'memo')";
stmt.executeUpdate(insert);
it produce an error like
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS(id,user_name,password,isAdmin,memo)VALUES(1,'admin','admin',1,'mem' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
String insert="INSERT INTO admin IF NOT EXISTS(id,user_name,password,isAdmin,memo)VALUES(1,'admin','admin',1,'memo')";
should be
String insert="INSERT IGNORE INTO admin (id,user_name,password,isAdmin,memo)VALUES(1,'admin','admin',1,'memo')";
MySQL (and any other SQL implementation as well) doesn't support IF NOT EXISTS in INSERT queries.
your INSERT query must be
"INSERT IGNORE INTO admin (id,user_name,password,isAdmin,memo) VALUES (1,'admin','admin',1,'memo')"
What you want may be INSERT ... ON DUPLICATE KEY UPDATE or INSERT IGNORE....
The former will update an existing row if a duplicate insert is detected, while the latter will just throw away duplicate inserts.
In both cases, you'll have to create a UNIQUE constraint on the column you want to check for duplicates. If the UNIQUE is violated, the alternate function is invoked.