I need to have few tables contained in a MySql db transferred to an Sqlite database through Java code.
For this, in my application, I do the following:
create an Sqlite db
select * from each table(of mysql db) , fetch data corresponding to each field from resultset, and insert data of each field to the corresponding sqlite table.
Is this the correct approach? Are there better methods of doing this?
Any help is appreciated.
Export the schema from MySQL with insert scripts, edit it (remove the MySQL-specific syntax), then execute it in SQLite.
If this is something that only happens once, you might consider using sqlite .import command (http://www.sqlite.org/sqlite.html) and using mysqldump to dump the data into a csv file that can be read by .import
Look at this python approach. may this can help you
https://gist.github.com/2972461
Related
I am newbie in hsqldb.
In the project I am using Spring 4, Hibernate 5 and HSQLDB.
I am having some specific task and I am trying to use HSQLDB as a portable database, which can be transferred to a flash drive or another computer.
I already have an sql-script with all tables and basic-needed data.
I have four questions that are haunting me.
(I'm sorry in advance if these questions are very stupid):
I need to make the script run at the first launch of the program, and in the other launсhes it must to check if database already exist and (if it already exists)only update data in it. (the program would be used in many computers and the database must be created after the first launch).
How can I do this? Is it possible? Can you give some basic advice or example about how I can to do that?
I am trying to find some information about saving all database info in some file in the file-system. Can you give me please some valid examples about saving hsqldb data in file and about using this file after another launch.
Can I place this file in my project.jar file and to work with all data from it update it e.t.c. ?
What is the best practice to make my database portable(for specific tasks) and where should I keep it? In file, in my project jar.e.t.c.?
Thanks in advance for your answers!
For data storage, you use a file: database. The JDBC connection URL is in the form jdbc:hsqldb:file:<file path>. HSQLDB will save all the data to file.
After connecting to the database you execute the SQL statements in your script one by one. If the tables already exist, the CREATE TABLE statements throw an error. This shows you don't have to execute them.
Because it's up to you when to keep the existing data and when to update it based on the existing contents of the database, you execute some SQL statements to decide. There is no automatic way to do this.
You can put an HSQLDB database in a jar but it cannot be updated. Jars are read-only.
The databases are fully portable. You can place them in a subdirectory of the user's home directory with the ~ symbol. See http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url and the rest of this page for details.
Is there a Java program to dump a table as a set of insert statements. I want to save state of a database table.
All I have is JDBC URL to DB which is located on network. Don't have Oracle installed thus no utilities and no SSH access either.
Looking for something similar to mysqldump or CSVWRITE in H2.
To save a state of a table you have several options:
Make a copy of the table data in another table.
Export using the oracle EXP or EXPDMP utilities.
Export into insert statements using external tools such as Toad,
SQLDeveloper (free from Oracle), PLSQL Developer. Or look for an online example and/or write your own.
It all depends on how you want to use the copy of your data and how much it is. For quick testing work a copy of the data in another table works best for me. With a simple sql script your table is back to it's original state.
I am using sql server, my issue is when I change any stored procedure or database table changes I need to make track of it and need to update it on each and every server when my related java code is uploaded.
So I am finding something tool or something if possible with java code that if I give stored procedure or database changes file(i.e .sql file) it executes it on sql server.
May be my question is silly but I really want to know about is this possible with any tool or through code?
If I understand you correctly, the scenario would be:
Read .sql file using one of the Readers
Execute SQL commands using JDBC and Statement class.
I cannot understand how distributing Java programs that use a database works.
Let's say I am using Derby as RDBMS and I want to store tasks and calendar entries in a database.
I want each user of the program to have a local database.
But I don't understand how in-memory databases are supposed to work. Should I write a script so that the first time my program is launched it creates a database and empty tables? Or will they be already created during the installation of the program?
If your program wants to store the user's tasks and calendar entries in a database, you probably don't want to use an in-memory database, because the in-memory database disappears when your program exits.
Rather, you want to use an ordinary persistent Derby database, which will store the user's data in files in a folder on the filesystem.
You do indeed have to create the database and issue the CREATE TABLE etc. statements to create the tables in that database. You could provide that as a separate script, or you could have your program issue those statements itself.
Tables are not automatically created, though; you have to issue the CREATE TABLE statements one way or another.
I want to save and load database on disk.
What I really want is to be able to do the typical application save as and open things with the database. Means when I want to save the database, I will click the save as button, and give a name to the database, and then save it. Later I want to be able to load back the database, by using open button to find the path to the database.
I'm using sqlite and java, and I heard that firefox bookmark manager using sqlite to store bookmarked data. And I don't know the correct term but roughly I want to be able to do like firefox bookmark manager to save and load the database.
Hope you guys can shed some light here.
You can use SQLite database files in two possible ways:
Like a database: Upon the first run of your application, you create the SQLite database file and create the schema (using CREATE TABLE SQL commands etc.). Then, whenever you want to change the saved data, you access your database file and execute single UPDATE, INSERT or DELETE statements to modify exactly those records that have changed. This makes operations such as Save as not quite straightforward, but it's comparably fast for large amounts of data where only small parts are modified.
Like a data file: Every time you save your data, you create a new database file (if there was one with the same name before, you delete it first). You then create the whole schema, and then you write all the information to the database file (using INSERT SQL statements). This allows you to handle things with the traditional Save/*Save as* commands.
For more detailed information, please ask more specifically; in particular, outline your problem if you need to know which approach serves you best.