Prabhat Kumar Singh
Prabhat Kumar Singh

Reputation: 1799

Trying to import a sql file to a remote Database via. c program

I am trying to achieve this by execlp().

The code is :

if(execlp("mysql","mysql","-e  \"source /single.sql\" -ppasswd --host 10.240.253.155 Dbname",(char*)0) == -1)

But I am getting this error:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check 
the manual that corresponds to your MariaDB server version for the right 
syntax to use near '"source /query.sql" -ppasswd --host 10.240.253.155 gdas

Though when I import the same file via terminal it works fine.

mysql -e "source ./query1.sql" -ppasswd -h10.240.253.155 dbname.

Usually this sql file contains only the Insert queries nothing else.

sample from the sql file:

INSERT INTO
gd_stats(client_IP,virtual_Host,ip_version,pOP,pOP_IP,pOP_Load,pOP_Hit_Stat,request_time,reply_time,time_req)
VALUES('10.240.253.10','service1.in',4,            
'NORTH_AMERICA','10.240.253.15',0,1,'04-20-2015  12:54:32','04-20-2015
12:54:32',7881);

I thought their might be a problem with this sql file(as it contains only Insert queries) so created a sql dump via mysqldump then tried to import that dump file but still same error.

The Database is present in the remote host.

I am using mariadb:

mysql  Ver 15.1 Distrib 5.5.41-MariaDB 

and trying to achieve this on rhel.

Upvotes: 0

Views: 97

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

You need to pass each argument to the command separately, right now exec Sends the whole string "-e \"source /single.sql\" -ppasswd --host 10.240.253.155 Dbname" as a single argument to the command. That is, the whole string will be passed in argv[1] to the mysql command.

So do instead

execlp("mysql", "mysql", "-e", "source /single.sql", "-ppasswd", "--host", "10.240.253.155", "Dbname", NULL)

Upvotes: 1

Related Questions