importing csv to db2 - csv

I am trying to import a .csv file with 2 columns (...and just 3 rows...just for testing) using the query:
IMPORT FROM "C:\db2\dtest.csv" OF DEL INSERT INTO TEST_DATA.DTEST (CAR, NICKNAME)
I am getting this error:
SQL0104N: SQL0104N An unexpected token "IMPORT FROM "C:\db2\dtest.csv"
OF DEL" was found following "BEGIN-OF-STATEMENT". Expected tokens may
include: "". SQLSTATE=42601
If I'm just being stupid here please do tell me :)

The IMPORT is not SQL, it is a command. That means you must submit the IMPORT from the Db2 command window (CLP), or from a script, or from a stored procedure . You cannot submit it directly via SQL, unless you use the ADMIN_CMD stored procedure. See the documentation for details and examples. You would use the ADMIN_CMD if you normally interact with databases via a GUI tool.
Using the command line is best ONLY if you will regularly use batch commands, or write scripts in any scripting language - and you are competent at scripting and using command-lines. But this method has pre-requisites especially if the database is on a different hostname or a different Db2-instance than the one you are working with (i.e. the db is remote). For remote databases the database needs to be catalogued via db2 catalog tcpip node .... and db2 catalog database ... commands.
Additionally you must first connect to the database via db2 connect to .... You have to do this regardless of whether the database is local or remote. See docs for details. For local databases you just use db2 connect to dbname where dbname must be your database name.
Using the ADMIN_CMD stored procedure if often easier for new users who are not familiar with using command line tools.
To use the Db2 command window (CLP), you can either use it interactively, or you can use your operating-system shell. On Windows use db2cwadmin.bat to open such a window (for batch or command usage), or run the db2.exe to enter interactive mode. You can then in interactive mode run your import command.

This happened to me when i was importing data from CSV to Sql DB
--In my case this happened because of incorrectly formatted CSV File.
When extracting data from CSV file have a look at the preview of data which will give an idea of where the data is wrong.
It may be because of different characters like , ' ' etc..
--t

Related

SQL SERVER Export Json to File

I want to export from sql server result json to 'json file'.
Example:
SELECT * FROM SYS.all_columns FOR JSON AUTO
I know one method to do this using the command "BCP".
You are invited to share different ways to export 'Json File From Sql Server.'
(Example convert you can watch Here: convert table to Json)
To much for a comment, but only half an answer...
You know BCP, so nothing to tell you here...
Important to know: SQL-Server is very limited in its access to the file-system. It is not running as the user running the script. it is running in the context of the machine, where the SQL-Server is running: So a file destination like c:\temp\SomeFile.json might not get in the so called directory on your machine. A destination somewhere on a shared drive might fail with access violations.
It might be more flexible to use an external tool (power shell or any programming language of your choice) to connect to the database and call the result in order to store it.
If you need to trigger this from within SQL-Server, you can execute such an external programm using xp_cmdshell (just as you do this with BCP.
I'd suggest to create a VIEW, an UDF or a SP to return whatever you need in a single call (with some paramterers). This will allow you to control the content from within SQL-Server but to execute the retrieval and the storage externally.
Hope this helps...
Enables system administrators to control whether the xp_cmdshell.
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE;
GO
Exec Your Query With bcp:
EXEC sys.XP_CMDSHELL 'bcp "SELECT * FROM SYS.all_columns FOR JSON AUTO;" queryout C:\Data\JsonTest.json -t, -c -S . -d master -T'

What is this string of text that looks like a URL for?

To be more precise why does it have this URL like form? It is used in my heroku appp my parsing out the host/username/passowrd/database which I use. But why does it look like a URL address.
Is mysql a protocol? I thought it was just a database or a command line executable in some cases.
I have found no where that I can use this URL like form that points to my database.
Even apps like sequel pro which are used to connect to mysql seem to have no use for it.
mysql://b77eeb5dd82foo:10d04foo#us-cdbr-iron-east-03.cleardb.net/heroku_37a39aa6115afoo?reconnect=true
That's a Data Source Name (DSN). As stated in the wiki
Software (e.g., Crystal Reports, Microsoft Excel, PHP, Perl, Python, Ruby) users can submit CRUD (Create, Read, Update, Delete) queries to a data source by establishing a connection to the DSN.
The format is
<driver>://<username>:<password>#<host>:<port>/<database>?<params>
mysql: means it's a MySQL database
b77eeb5dd82foo:10d04foo is the username and password
us-cdbr-iron-east-03.cleardb.net is the server name
heroku_37a39aa6115afoo is the database name
reconnect=true is a parameter telling the driver to reconnect automatically when necessary
Most of the fields are optional; the port number is omitted from your DSN, so it uses the default (3306 for MySQL).

importing a database into phpmyadmin #1044 - Access denied for user

I have recently purchased web hosting service at godaddy. I have already completed my project and i used xampp. Now i want to import the database which i arleady have. I tried to do it in the normal way by clicking on import and choosing the file. But i'm getting this error
How can I fix this?
Many web hosts only give you access to one database. Your SQL file is trying to create a new database (with a line near the top like CREATE DATABASE `socialnew`... as you see in the error message).
The likely solution is to not create the database from your script. You can either edit the .sql file manually and remove the lines starting with CREATE DATABASE `socialnew`... and USE `socialnew`;, or, if your phpMyAdmin is pretty up-to-date, you could do the export again from your XAMPP and make sure the checkbox is not selected for "Add CREATE DATABASE / USE statement".
Then when doing the import, make sure you're selecting the Import tab from within the database you wish to import to.
You may also open the .sql file in an IDE and just delete the create command.Then, assuming you've navigated to the desired DB import section in phpMyAdmin, you can run the import with the modified file and you should be good to go.
Must Ensure that your User_Name, Password and Database name are correct. If you are Deal with database 'sampled' then type 'sampled' in query instead of 'db' also must ensure that you don't have to use quota(') in Statement. For More and Accurate Detail visit Following link: http://onephin.com/1044-access-denied-user-user_namelocalhost-database-database_name/
Export and import just tables of the database:
-> open the database in phpmyadmin
-> click to export
-> save the file (you don't have to change any settings)
-> open the database on new server
-> click to import
-> chose the file
-> click to "GO" and you are done...
It was working for me :-)

Import MySQL database with phpMyAdmin

I'm getting an error:
#1044 - Access denied for user 'host_adminman'#'localhost' to database 'testdb4'
I thing that it should be denied, since the user/pass combo for MySQL 5.6 that was used on my PC while developing DB do not match ones I got from host. I've found explanation from HostGator that:
"The problem here is your import file contains an SQL query that attempts to create a database for the wrong username. Notice the user2 in 'user2_wrdp9' does not match the username1 in 'username1'#'localhost'. Someone must edit the import file and change the old user2 to your new username1.
So, how do I do that?
I have MySQL Workbench installed. Added, the same username as on the host, gave it all privileges to 'testdb4', exported database in a SQL file, but I still get this error when I try to import it on the host.
Did somebody use phpmyadmin to create this import file?
If so, can you get them to do it again?
If so, can they use the "custom-display all possible options" item in the phpmyadmin export panel? That will allow them to uncheck the "Add CREATE DATABASE / USE statement" and redo the export. That should get rid of your problem with the import.
If you can't get the export file remade, you can use a simple text editor (for example, the notepad program on Windows) to fix up the import SQL file.
If the filename ends in .zip or .gz or some such, you need to uncompress it first.
Then open it up in the text editor. Right near the beginning of the file you'll find lines like these:
--
-- Database: `blah_blah`
--
CREATE DATABASE IF NOT EXISTS `blah_blah` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `blah_blah`;
Delete them and save the file. Then try importing it again.

MySQL Errorcode 2 in Perl script

I have created an ETL tool in Perl. There are three database servers to which ETL tool is communicating, say dbserver1 (OLTP server - Windows Box), dbserver2 (staging server - linux Box), dbserver3 (OLAP Server, linux Box). My ETL script is on dbserver 2.
The scripts reads the data from dbserver1 and bring it into the dbserver2 for some trnsformations, performs transformations and then put the data into the dbserver3. For achieving this the script creates some OUTFILE data on dbserver2. So there are two type of OUTFILE queries:
OUTFILE query which runs on the dbserver1, creates .data on dbserver2 and
OUTFILE query which runs on the dbserver2 creates .data file on dbserver2.
The second query works fine as it is creating a file on the same server. But first type of query gives me following error:
DBD::mysql::st execute failed: Can't create/write to file '\home\dbserver2\dumpfile.2011-11-04-03:02.data' (Errcode: 2) at stagingtransform.pl line 223, <> line 8.
I guess this is related to some user permissions. And if I am not wrong then, MySQL on dbserver2 is having permissions to read/write to the dbserver2, but MySQL on dbserver1 is not.
Can it be because of the dbserver1 is Windows and dbserver2 is Linux box?
How can I resolve this?
FYI: The file formate is: dumpfile.yyy-mm-dd-hh:mm.data and I also have set the AppArmor settings for MySQL on dbserver2, which is for MySQL on dbserver2.
The problem is the outfile query on dbserver1 can only write locally, so you need a different approach.
One very easy method is to use mysqldump (on dbserver2) to connect to dbserver1 and pipe the output to a mysql client which injects the SQL into dbserver2.
On the other hand, if you want to use DBI:
my $source_sql = q{SELECT ...};
my $target_sql = q{INSERT ... VALUES (?, ?, ...)};
my $source = $source_dbh->prepare($source_sql);
my $target = $target_dbh->prepare($target_sql);
$source->execute;
my $qty = $target->execute_array({ArrayTupleFetch => $source});
For large transfers of data, the mysqldump approach is faster.