Inserting to Sqlite is slow [duplicate] - java

This question already has answers here:
Improve INSERT-per-second performance of SQLite
(12 answers)
Closed 8 years ago.
I am new to Database.
I am tying to insert 22000 word to a sqlite DB but it is taking forever to complete
for (int i=0;i<s.size();i++){
String sql = "INSERT INTO WORD (word) VALUES ("+"'"+s.get(i)+"'"+");";
statement.executeUpdate(sql);
}
Table name is WORD and field name is word(which is a String)
Here s is an arrylist and s.get(i) returns a string... I have made the connection and trying to update
the table.
It is very slow. What am I doing wrong?

Its slow because you are firing 22000 queries to the database. You should insert in batches. That will fix the problem. Even if a single insert takes 10ms total time is like 3.5 minutes!!
A simple insert query with batches of 2 will look like this
"INSERT INTO WORD (word) VALUES ("+"'"+s.get(i)+"'"+"), ("+"'"+s.get(i+1)+"'"+");";
This will reduce the time by half. Now you will have to programatically insert more of these values.

Related

Passing variable into IN clause in java [duplicate]

This question already has answers here:
Hibernate Criterion IN Clause 1000 break up
(3 answers)
Java Oracle exception - "maximum number of expressions in a list is 1000"
(7 answers)
Closed 5 years ago.
I am constructing a hibernate query and I pass a list of String values into the IN clause. That list sometimes happens to be more than 1000 values so I receive an error. I have looked up some solutions like breaking that clause into smaller ones or making temporary table, but none of them showed how actually it is better to work with variable list.
So if I had something like:
SELECT * FROM MY_TABLE WHERE NAME IN (list).
What would be the best way to handle this?

How to get data from sqlite in order by text date in android [duplicate]

This question already has answers here:
Sorting dates in sqlite database?
(3 answers)
Closed 6 years ago.
I have stored date in sqlite database as text in this format : '1 Jan 99'.
Now I want to get data from sqlite database in order by date.
Already tried :
SELECT * FROM Table ORDER BY date(dateColumn) DESC
Select * from Table order by datetime(datetimeColumn) DESC
Please Help!!
try the requet SELECT * FROM Table ORDER BY dateColumn

create any table name with prepareStatement jdbc java [duplicate]

This question already has answers here:
JDBC prepareStatement doesn't work
(4 answers)
Closed 7 years ago.
So I'm trying to create an unspecified table name with an unspecified columns, however, I'm new to prepareStatement and I'm not exactly sure what to do.
This is how I'm thinking about it
I know I'll need a loop for multiple entries of "line" which is the table name
but how can I deal with the columns? I think I'm specifying the number of columns in here (4). How can I do this without specifying? and what should I put for setString if the value will differ based on the table name?
I'm kind of confused and I was hoping that someone will explain this to me
loop start ....
line = kb.next();
sql = "Create TABLE " + line + "(?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,?);
pstmt.executeUpdate();
loop end
It's unlikely that you'll be able to get this to work on any database.
For Oracle-specific reasoning, see Why cannot I use bind variables in DDL?

How can I avoid duplicates in my "mysql" database when using hibernate (JAVA)? [duplicate]

This question already has answers here:
Best way to avoid duplicate entry into mysql database
(4 answers)
Closed 7 years ago.
I parse a .lst file to get data, then I use hibernate in order to add these data in a mysql database.
My file is always updated with new data but it KEEPS existing ones.
The question is how can I avoid duplicates data in my database when I parse the file a scond time.
Thanks
INSERT INTO table_name ('your fields') VALUES ('New values') ON DUPLICATE KEY UPDATE field1=' value',field2='value',.....;
Table Structure:
make this 2 fields (name and city )Uniqe
ex:
INSERT INTO tbl_stud(name,city)values('Divyesh','Ahmedabad') ON DUPLICATE KEY UPDATE name='Divyesh',city='Ahmedabad';

Passing string array to a prepared statement [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
java PreparedStatement
Can I make the prepared statement
SELECT * FROM STUDENTS WHERE STUDENT_ID IN ?
into
SELECT * FROM STUDENTS WHERE STUDENT_ID IN (1,2,3)
Given that the collection of student ids is a string array.
I think your best solution is going to be generate the in clause dynamically: IN (?,?,?,?) , and then calling preparedStatement.setInt(i + 1, myValues[i]) for each value in your array/collection. The bad news is you will end up with a different preparedStatement for each time you have a different number of values.
See duplicate question: PreparedStatement IN clause alternatives?

Categories

Resources