Exporting MySQL table to .csv file - mysql

This is my first day with VBA. I am trying to export a .csv file in MS Access using MySQL query in VBA. I have tried using this code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sql As String
Set db = CurrentDb
sql = "SELECT wo_id FROM WorkOrder"
Set rs = db.OpenRecordset(sql)
DoCmd.TransferText acExportDelim, , "rs", "C:\export.csv", True
rs.Close
Set rs = Nothing
which gives me:
Run time error '3011':
The Microsoft Office Access database engine could not find the object 'rs'. Make sure the object exists and that you spell its name and path name correctly
MORE INFO
I have a MySQL table called WorkOrder (I would like to pull data from here).
What am I missing?

You can only export MS-Access objects (tables or queries) using DoCmd.TransferText. If you have a query called qryOutput you can export it:
DoCmd.TransferText acExportDelim, , "qryOutput", "C:\export.csv", True
You could create the query on runtime (using db.CreateQueryDef), export it and delete it.
If you are working with MySQL, maybe it is easier to export the data directly from the command line:
c:\> mysql -h YourHost -u YourUser -pYourPassword dbYourDatabase -e"SELECT wo_id FROM WorkOrder" > c:\export.txt
This will create a tab-separated text file with the result of your query (including headers).
On a Unix-like command line you could convert this output on-the-fly to a comma separated text file using sed:
$ mysql [connectionParameters] -e"select..." | sed 's/\t/,/g' > export.csv
I don't know if there are any pure-windows command line utilities to do this. I use Cygwin for this kind of things. If you want to use it, be sure to install sed using Cygwin Setup program.
Hope this helps.

Related

MySQL Date_Format return "i" in Excel

I'm trying to make a simply batch that will run a query and generate a .xls file with selected data.
The issue is that in my query i use DATE_FORMAT(ORAIN_PTV, '%H:%i') AS ORA which in MySQL return the correct value of "HH:mm" / ex: "13:30" while when the data is exported in excel it look like a simple "i" char.
How can i fix that issue?
here is what my batch file looks like:
cd C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin
mysql -u root -pxxx -e "SELECT DESCLI_PTV AS NOME, DATAIN_PTV AS DATA, DATE_FORMAT(ORAIN_PTV, '%H:%i') AS ORA, NUMCOP_PTV AS COPERTI, TELCLI_PTV AS TELEFONO, EMACLI_PTV AS EMAIL FROM myristo.prenota_tavoli WHERE DATAIN_PTV = CURDATE();" > C:\Users\imytyuk\Desktop\PRENOTAZIONI.xls

Incorrect syntax when import sql file from MySQL to MS SQL via SQLCMD

I have large .sql files exported from MySQL, and try to import them to MS SQL(localDB) via
SQLCMD. But when I type in the following into Command-prompt:
sqlcmd.exe -S (localdb)\MSSQLLocaldb -i
C:\Users\Administrator\Desktop\1\SQLQuery4.sql
I got the following error message:
Incorrect syntax near 'tblo'
I checked my .sql file, it seems SQLCMD can't understand double quotes
e.g.
INSERT INTO "tblo" VALUES (2,'DTT','10000286','Dp','y',2,38,'2010-02-22
11:03:51','2010-02-22 11:03:51');
However, it's fine with SSMS
Any idea to solve this problem?
I found a solution by myself:
I can add --skip-quote-names flag when I dump data from MySQL
e.g.
mysqldump.exe -hlocalhost -uUserName -pPassword --compatible=mssql --no-create-info --skip-quote-names --skip-add-locks DataBase tblo > D:\Test\dump.sql
Result in dump.sql will be like:
INSERT INTO tblo VALUES (2,'DTT','10000286','Dp','y',2,38,'2010-02-22 11:03:51','2010-02-22 11:03:51');
So I can use this .sql to directly import data into MS SQL server via SQLCMD
sqlcmd -S (localdb)\MSSQLLocaldb -i D:\Test\dump.sql

Exporting mysql database from wampserver using vb6

Im having a problem exporting a mysql database from wampserver. Im using the following code:
Private Sub cmdBackup_Click()
Screen.MousePointer = vbHourglass
DoEvents
cmd = Chr(34) & "C:\Documents and Settings\Owner\Desktop\Documents\Clients\Darwin\mysql\mysqldump" & Chr(34) & "mysqldump.exe -uroot -p -hlocalhost school > c:\a.sql"
Call execCommand(cmd)
Screen.MousePointer = vbDefault
MsgBox "Done"
End Sub
As expected, an .sql file was created in my C:/. But for unknown reason, the created sql file is empty (literally empty, 0 byte, no texts inside).
Can someone help me? Thanks in advance! Sorry for my english.
I have no idea if that code should work its been years since I used VB6 but one obvious problem I can see is that when you execute this statement,
mysqldump.exe -uroot -p -hlocalhost school > c:\a.sql
the mysqlsump.exe will see the -p parameter and be expecting a password to be entered from the console. Try it manually from a command window!
I am guessing there is no console and if there was the user should not know the password to the root account anyway.
The quick solution would be to change the code to:
mysqldump.exe -uroot -pSuperUsersPassword -hlocalhost school > c:\a.sql
but that is a major security nightmare as you have hardcoded the MySQL Super Users password in a piece of code. Remember MySQL is a proper DBMS and can manage many different databases, not just the one you are using.
Also if the password is ever changed you must remember to change this code at the same time, a maintenance nightmare.
At the very least you should create a new user account in MySQL that has access to only this one database and has only the privilages to do backups (SELECT, SHOW DATABASES, LOCK TABLES, EVENT). Then at least you are not giving away the Crown Jewels to anyone that can see your source code or can read a hex dump of your executable.

Create MySQL Database with .SQL File

I don't know much about MySQL at all. But I am trying to reverse engineer a MySQL database using Visio. I know what steps I should take to do this, but I can't get my database to show in the 'Databases' section (as below):
How do I create the MySQL database using the .SQL file and get it to show up in this list? I have tried this code:
mysql -u username -p password database_name < filename.sql
using my own credentials of course. But that doesn't seem to work. In what folder should the .SQL file be placed if this statement is to work?
1) Create a file "filename.sql"
2) Create a database in your DB in which you want to import this file.
3) From command-prompt/terminal, move to the directory where you have created a "filename.sql".
4) Run the command: mysql -u username -p password database_name < filename.sql. (You can also give the proper path of your file and run this command from anywhere). It might be the case that you don't have a password set for MySQL. If so, mysql -u username database_name < filename.sql will also work.
In your case if you have created a database with name ojs and also created a file with name ojs.sql in C: drive then run the following command:
Edit: Put the path inside quotes.
mysql -u username -p password ojs < "C:\ojs.sql"
There is another way of importing tables in mysql. You can do it this way as well:
1) Connect your database
2) Type command "use ojs;"
3) Type command "source C:/ojs.sql"
Most MySQL SQL files that create databases create the database 'on-the-fly', so you typically needn't do anything except:
log-in
mysql -u [username] -p[password]
(Note: make sure you do NOT include a space (' ') character between the -p and the [password].
MySQL will think that [password] is the name of the database you want to connect to.
The 'general' log-in (above) does not assume you want to connect to any particular schema.)
source the file (do not use quotes around filename)
mysql> source [database_creation_file].sql
you can simply do it using mysql workbench
1> create a new query tab
2> CREATE DATABASE database_name;
3> USE database_name;
4> open the filename.sql file and execute it ctrl + shift + enter
5> all the tables in the filename.sql are created
To create a MySQL database using a SQL file, you can follow these steps:
Log in to your MySQL server using the mysql command-line tool and the appropriate credentials.
Use the CREATE DATABASE command to create a new database with the desired name:
CREATE DATABASE database_name;
Use the USE command to switch to the newly created database:
USE database_name;
Use the SOURCE command to import the SQL file into the database:
SOURCE path/to/sql/file;
The database will now be created and populated with the data from the SQL file. You can verify this by running some SQL queries against the database.
It's important to note that this process assumes that the SQL file contains valid SQL statements compatible with the version of MySQL you are using. If the SQL file contains any errors or unsupported statements, they will be displayed in the mysql command-line tool, and the import process will be interrupted.

convert sqlite file into mysql

I have a .sqlite file which I want to convert into mysql. So is there any way to convert it into mysql?I am using ubuntu. So is there any shell script to change it. Any help will be highly appreciable.
assuming your sqlite db file is login.db
download this script as sqlite3-to-mysql.sh
this command will tranform your sqlite db file to standard mysql sql file: sqlite3 login.db .dump > sqlite.sql && bash sqlite3-to-mysql.sh sqlite.sql > mysql.sql && rm sqlite.sql
import to mysql: mysql -uuser -ppassword -D database < mysql.sql
I've tried this recently and most answers don't work because they're out of date. e.g. python vs python3
A method that worked for me that should never go out of date is to export the tables to CSV and then import them as CSV. I used Sequel Pro which can import a CSV. The downside is that you need to do each table individually. The upside is that this is a very robust method and doesn't require any custom scripts.
Run the following in your database folder changing "thedatabase" and the two occurances of "thetable" to the database name and table name respectively:
sqlite3 -header -csv thedatabase.db "select * from thetable;" > thetable.csv
For Sequel Pro:
Create your database
(Optional: Create table structure. Recommended to have the correct column types) You could get the create statement from an sqlite dump and fix them for mysql.
File > Import
Format: CSV
Check: "First line contains field names"
Check the field mappings and import
Explanation of the command to export:
"sqlite3" is the command line client for sqlite databases.
"-header -csv " sets the output to csv
"thedatabase.db" is the database file
"select * from thetable;" is the query to select all data from the table
" > thetable.csv" redirects the output of the data to thetable.csv file
1) Export sqlite database to sql file
sqlite3 sqlite.db .dump > mysql.sql
2) Import mysql.sql file to mysql database using phpmyadmin or other tools.
To directly import it to the database in just one command:
sqlite3 database.db .dump | mysql -uuser -ppassword database
Google "sqlite to mysql" will give you a lot of articles doing that ...
Okay, easiest is to open the .sqlite file using sqlite, .dump to a file, and the file is a text file containing SQL statements.
You shall be able to edit that file and run in your mysql DB thereafter.