Inserting CSV File into MYSQL DB via Windows Powershell - mysql

I'm trying to fill my db with a csv file via windows powershell and I'm getting following error:
Ausnahme beim Aufrufen von "ExecuteNonQuery" mit 0 Argument(en): "ERROR [42000] [MySQL][ODBC 8.0(a) Driver][mysqld-8.0.29]You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '{Filiale=0; Jahr=2010; Auftrag_Monat=9; Auftrag_Woche=38; LE_Jahr=0; LE_Monat=0;' at line 1"
In C:\PowerSSkripte\CSV Insert.ps1:28 Zeichen:5
+$stmt.ExecuteNonQuery()
This is my code:
$Conn = new-Object System.Data.Odbc.OdbcConnection
$Conn.ConnectionString = "DB-Connection"
$Conn.Open()
$query = "DELETE FROM akustik"
$stmt = New-Object System.Data.Odbc.OdbcCommand($query,$Conn)
$stmt.ExecuteNonQuery()
The delete is working as intended!
Now I'm trying to insert the csv into my db:
$data = Import-Csv C:\TextExport\Akustik.csv
$query = "INSERT INTO akustik (Filiale, Jahr, Auftrag_Monat, Auftrag_Woche, LE_Jahr, LE_Monat, LE_Woche, Anzahl, Anzahl0, Umsatz) VALUES ($row.Filiale, $row.Jahr, $row.Auftrag_Monat, $row.Auftrag_Woche, $row.LE_Jahr, $row.LE_Monat, $row.LE_Woche, $row.Anzahl, $row.Anzahl0, $row.Umsatz)"
$stmt = New-Object System.Data.Odbc.OdbcCommand($query,$Conn)
foreach($row in $data[0]){ #for testing i want only to insert the first row of my csv so im using data[0]
$stmt.ExecuteNonQuery()
}
I'm getting following error.
error:
Ausnahme beim Aufrufen von "ExecuteNonQuery" mit 0 Argument(en):
"ERROR [42000] [MySQL][ODBC 8.0(a) Driver][mysqld-8.0.29]You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '{Filiale=0; Jahr=2010; Auftrag_Monat=9; Auftrag_Woche=38; LE_Jahr=0; LE_Monat=0;' at line 1"
In C:\PowerSSkripte\CSV Insert.ps1:28 Zeichen:5
$stmt.ExecuteNonQuery()
row 28 is: $stmt.ExecuteNonQuery() the syntax should be fine cause its the same when I'm inserting sth. into my db via MYSQL Workbench.
When I'm changing my insert statement to:
$query = "INSERT INTO akustik (Filiale, Jahr, Auftrag_Monat, Auftrag_Woche, LE_Jahr, LE_Monat, LE_Woche, Anzahl, Anzahl0, Umsatz) VALUES (99, 3002, 12, 12, 3002, 12, 12, 12, 12, 250000.000)"
everything is working fine....
I hope somebody can help me to fix this little issue.
Thanks in advance.
Greetings littleStudent

Related

Saving dbplyr query (tbl_sql object) to MySQL without saving data locally

This question expands on this question
Here, I'm using the custom function created by #Simon.S.A. shown in the answer to this question. I'm attempting to save a tbl_sql object in R to MySQL as a new table without first saving it locally. Here, the database and schema in my MySQL are named "test." The tbl_sql object in R is my_data, and I want to save this is a new table in MySQL labeled "car_data".
library(DBI)
library(tidyverse)
library(dbplyr)
#establish connection and import data from MySQL
con <- DBI::dbConnect(RMariaDB::MariaDB(),
dbname = "test",
host = "127.0.0.1",
user = "user",
password = "password")
my_data <- tbl(con, "mtcars")
my_data <- my_data %>% filter(mpg >= 22)
# write function to save tbl_sql as a new table in SQL
write_to_database <- function(input_tbl, db, schema, tbl_name){
# connection
tbl_connection <- input_tbl$src$con
# SQL query
sql_query <- glue::glue(
"SELECT *\n",
"INTO {db}.{schema}.{tbl_name}\n",
"FROM (\n",
dbplyr::sql_render(input_tbl),
"\n) AS sub_query"
)
result <- dbExecute(tbl_connection, as.character(sql_query))
}
# execute function
write_to_database(my_data, "test", "test", "car_data")
After running final line, I get the following error. I'm not sure how I can fix this.
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.test.car_data
FROM (
SELECT *
FROM `mtcars`
WHERE (`mpg` >= 22.0)
) AS sub_quer' at line 2 [1064]
12.
stop(structure(list(message = "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.test.car_data\nFROM (\nSELECT *\nFROM `mtcars`\nWHERE (`mpg` >= 22.0)\n) AS sub_quer' at line 2 [1064]",
call = NULL, cppstack = NULL), class = c("Rcpp::exception",
"C++Error", "error", "condition")))
11.
result_create(conn#ptr, statement, is_statement)
10.
initialize(value, ...)
9.
initialize(value, ...)
8.
new("MariaDBResult", sql = statement, ptr = result_create(conn#ptr,
statement, is_statement), bigint = conn#bigint, conn = conn)
7.
dbSend(conn, statement, params, is_statement = TRUE)
6.
.local(conn, statement, ...)
5.
dbSendStatement(conn, statement, ...)
4.
dbSendStatement(conn, statement, ...)
3.
dbExecute(tbl_connection, as.character(sql_query))
2.
dbExecute(tbl_connection, as.character(sql_query))
1.
write_to_database(my_data, "test", "test", "car_data")
Creating a table with INTO command is an SQL Server (even MS Access) specific syntax and not supported in MySQL. Instead, consider the counterpart statement: CREATE TABLE...SELECT. Also, schema differs between RDBMS's. For MySQL, database is synonymous to schema.
Therefore, consider adjusted version of SQL build:
sql_query <- glue::glue(
"CREATE TABLE {db}.{tbl_name}\n AS \n",
"SELECT * \n",
"FROM (\n",
dbplyr::sql_render(input_tbl),
"\n) AS sub_query"
)

SQL % statement in python returns error

I created 5 tables in mysql workbench 5.7 in which I will pull data from APIgraph queries for a given facebook page.
However, when I run the code, it throws an error:
ProgrammingError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s)' at line 1
Here is the part of the code which I think contains the error :
#create connection to db
connection = connect_db()
cursor = connection.cursor()
#SQL request for inserting the date of the page into the database
insert_page = ("INSERT INTO page"
"(fb_id, name)"
"VALUES (%s, %s)")
insert_posts = ("INSERT INTO posts "
"(page_id, fb_post_id, message, time_created)"
"VALUES (%s, %s, %s, %s)")
And I finally put the data at the end of the code:
cursor.execute(insert_page, json_pageiddata)
Any ideas? Thanks for helping
EDIT: here is my json_pageiddtata variable, obtained from a URL query with APIgraph:
pageid_url = create_pageid_url(current_page, APP_ID, APP_SECRET)
json_pageiddata = render_to_json(pageid_url)
print json_pageiddata["name"], json_pageiddata["id"]
If you are using data as dictionary u need to specify index name, try to use %(name)s.

What does this mysql error means?

I get the following error:
1064 You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'AND `hackcount` >= 3' at line 1 SQL=SELECT COUNT(*) from
`xxxxx_mi_iptable` WHERE ip = AND `hackcount` >= 3
What does this mean? What should I do to fix this?
There's an error in your WHERE clause:
WHERE ip = AND `hackcount` >= 3
Where ip equals what, exactly? You forgot to put a value there.
Looks like you're using Joomla plugin "Marco's SQL Injection" (http://www.mmleoni.net/sql-iniection-lfi-protection-plugin-for-joomla).
It has a line that tries to detect the IP of the current client
$remoteIP = $_SERVER['REMOTE_ADDR'];
Because REMOTE_ADDR is not always present/reliable/sufficient the following SQL query
$sql = "SELECT COUNT(*) from `#__mi_iptable` WHERE ip = '{$remoteIP}' AND `hackcount` >= {$this->p_ipBlockCount}" ;
fails.
the plugin should use something like this
if (isset($_SERVER['REMOTE_ADDR']))
{
$remoteIP = $_SERVER['REMOTE_ADDR'];
}
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$remoteIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif (isset($_SERVER['HTTP_CLIENT_IP']))
{
$remoteIP = $_SERVER['HTTP_CLIENT_IP'];
}

Inserting multiple rows in a column in MySQL using a Perl program

I want to insert multiple rows of data in a single column using a single query. This program is for less data. I have another weather monitoring .txt file which had 4000 lines of data. I can insert one data at a time but it becomes tedious for so many data values.
1. use DBI;
2. use DBD::mysql;
3. use warnings;
4. $connection = ConnectToMySql($database);
5. # Multiple Data inputs
6. $myquery = "INSERT INTO data(datatime,battery)
7. VALUES
8. (?,?),
9. ('16.01.2013','6.54'), #data corresponding to date and battery
10. ('17.01.2013','6.42'),
11. ('21.01.2013','6.24'),
12. ('22.01.2013','6.21'),
13. ('24.01.2013','6.17'),
14. ('25.01.2013','6.13'),
15. ('28.01.2013','6.00'),
16. ('29.01.2013','5.97'),
17. ('30.01.2013','5.94'),
18. ('01.02.2013','5.84')";
19. $statement2 = $connection->prepare($myquery);
20. $statement2->execute($myquery);
21. #--- start sub-routine
22. sub ConnectToMySql {
23. $database ="xxxx";
24. $user = "XXXX";
25. $pass = "XXXX";
26. $host="XXXX";
27. my $dbh = DBI->connect("DBI:mysql:$database:$host", $user, $pass);
28. }
This code is giving me the following errors:
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2 at C:/Users/User/workspace/DataBaseEntry/DataEntry.pl line 20.
DBD::mysql::st execute failed: called with 1 bind variables when 2 are needed at C:/Users/User/workspace/DataBaseEntry/DataEntry.pl line 40.
I cannot identify the problem. Is it the placeholder. What can i do to improve it?
I am new to these things. so can you keep it simple.
THANKS
You should be passing the data values which should replace the (?, ?) as parameters to execute. Your code as written only passes a single parameter to execute and that parameter is the SQL text of your query.
Try this instead:
$myquery = "INSERT INTO data(datatime,battery) VALUES (?,?)";
my $sth = $connection->prepare($myquery);
$sth->execute('16.01.2013','6.54');
$sth->execute('17.01.2013','6.42');
$sth->execute('21.01.2013','6.24');
$sth->execute('22.01.2013','6.21');
$sth->execute('24.01.2013','6.17');
$sth->execute('25.01.2013','6.13');
$sth->execute('28.01.2013','6.00');
$sth->execute('29.01.2013','5.97');
$sth->execute('30.01.2013','5.94');
$sth->execute('01.02.2013','5.84');
$connection->do(<<'EOT');
INSERT INTO data (datatime, battery)
VALUES
('17.01.2013', '6.42'),
('21.01.2013', '6.24'),
('22.01.2013', '6.21'),
('24.01.2013', '6.17'),
('25.01.2013', '6.13'),
('28.01.2013', '6.00'),
('29.01.2013', '5.97'),
('30.01.2013', '5.94'),
('01.02.2013', '5.84')
EOT
I'm not sure what you're trying to do with placeholders here.
Also, you're missing use warnings; use strict;, and you shouldn't use global variables everywhere.

Python 3, SQLAlchemy, MySQL. Incorrect syntax in INSERT statement

Engine = create_engine("mysql+mysqldb://blah-blah-blah", encoding="utf-8")
Session = sessionmaker(bind = Engine)
ses = Session()
Meta = MetaData(bind = Engine, reflect = True)
PersonTable = Meta.tables["person"]
class Person(object):
pass
mapper(Person, PersonTable)
APerson = Person("1111", "2222", "1.01.1980")
ses.add(APerson)
ses.commit()
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s, %s)' at line 1") b'INSERT INTO person (Name, OriginalName, DoB) VALUES (%s, %s, %s)' ('1111', '2222', '25.01.1980')
What is the %s? What do I wrong?
Python 3.1
SQLAlchemy 0.6.5
MySQL 5.1
Windows 7 Ultimate
Thank you.
You sqlalchemy commit is trying to issue an insert query that is not compatible with the schema. near '%s, %s, %s)' means your trying to insert invalid data. I can only speculate that it is because the date format - this is not the proper mysql date format YYYY-MM-DD.
I migrated back to Python 2.7. Now it works fine.