Error when inserting into Database in Python - python-3.6

What is wrong with the code to produce this error
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%s)' at line 1
I am using XAMPP Localhost Database and mysql.connector Package in Python. My Code for Inserting:
cursor = db.cursor()
username = input()
sql = "INSERT INTO `employee` (`id`, `username`) VALUES (NULL, %s)"
cursor.execute(sql, username)
db.commit()
print("success")

Instead of this line
cursor.execute(sql, username)
try this
cursor.execute(sql, [username])
execute expects a list/(list of tuples) as the second parameter. You can have a look at the documentation if you need more details.

Related

MYSQL Update errors

Getting errors on the following. seems simple, but can't clear this error:
NOTE: that wphz_posts.guid is a text field, not int
All table and column names are correct
"SQL Error (1064): 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 ' WHERE wphz_postmeta.meta key = ‘ProductURL’ AND
wphz_postmeta.post id = wphz.. at line 3
UPDATE wphz_posts
SET wphz_posts.guid = (select concat(wphz_postmeta.meta_value,"?campid=nnnnnn")
WHERE wphz_postmeta.meta_key = 'ProductURL' AND wphz_postmeta.post_id = wphz_posts.id)

Mariadb syntax error on Declare statement

I am creating a MariaDB stored procedure in phpmyAdmin and I keep getting a syntax error.
I have tried appending an # symbol for the variable but I am still getting the error.
Here is what my code currently looks like:
DECLARE #cNAME = VARCHAR(100);
SET #cNAME = "TEST";
SELECT * from tblUser where UserName = cNAME;
I expect that my stored procedure would get saved but it doesn't because of the syntax error.

OperationalError: near "(": syntax error" at SQLite3

After taking a crash course for SQLite3, I tried to make a db for my first project:
import sqlite3 as db
conn = db.connect('todo.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE todo(id serial primary key, title text, created
timestamp default now(), done boolean default 'f')")
cursor.execute("INSERT INTO todo (title) VALUES('Learn web.py')")
Unfortunately I receive this error:
OperationalError: near "(": syntax error" at SQLite3
I do not understand what's wrong with the code. Can anyone explain what I am doing wrong?
As shown in the documentation, if the default value is not a simple value, it must be enclosed in parentheses:
CREATE TABLE todo(
...,
created timestamp default (now()),
done boolean default 'f'
);
(And 'f' is not a valid value for a boolean. And now() is not an SQLite function.)

How to use RowMapper to insert record into table?

How to insert data into table using row mapper?
I am trying this:
Employee user1 = jtemplate.queryForObject("INSERT INTO employee(id, name,salary) VALUES(10,'ABC',12333);",new BeanPropertyRowMapper<Employee>(Employee.class));
It gives me bad SQL grammar error.
But query works in SQL developer. What I am doing wrong?
Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [INSERT INTO employee(id, name,salary) VALUES(99,'ABC',12333)]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:411)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:466)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:476)
at com.cts.orm.rowmapper.Test.main(Test.java:29)
Caused by: java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:389)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:382)
at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:600)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:450)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
at oracle.jdbc.driver.T4C8Odscrarr.doODNY(T4C8Odscrarr.java:98)
at oracle.jdbc.driver.T4CStatement.doDescribe(T4CStatement.java:805)
at oracle.jdbc.driver.OracleStatement.describe(OracleStatement.java:3978)
at oracle.jdbc.driver.OracleResultSetMetaData.<init>(OracleResultSetMetaData.java:55)
at oracle.jdbc.driver.OracleResultSetImpl.getMetaData(OracleResultSetImpl.java:175)
at org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:240)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:455)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:400)
... 3 more
NamedParameterJdbcTemplate does what you want:
namedParameterJdbcTemplate.update(
"INSERT INTO employee(id, name, salary) VALUES (:id, :name, :salary)",
new BeanPropertySqlParameterSource(Employee.class));
removing the semi colon at the end of the statement should solve the problem use it like this. when using the jdbc templates it is not good to use semicolon at the end of sql statements
Employee user1 = jtemplate.queryForObject("INSERT INTO employee(id, name,salary) VALUES(10,'ABC',12333)",new BeanPropertyRowMapper<Employee>(Employee.class));
try using it like
jtemplate.update("INSERT INTO Employee(ID, NAME, Salary) VALUES (?, ?, ?)",
new Object[] { employee.getId(), employee.getName(), employee.getSalary() });
i am using this and it works correctly and displays no errors
Try this:
jdbcTemplate.update("INSERT INTO employee(id, name,salary) VALUES(?,?,?)", 10, "ABC", 12333);

Sqlite DB Error - could not prepare statement

I am getting following error on insert statement for sqlite DB
could not prepare statement (1 near "undefined": syntax error)
I tried 2 variations of insert, for both error is same
var sql = "INSERT INTO Med(MedID) VALUES(?),";
sql += "['"+dataObj[i].MedID+"']";
var sql = "INSERT INTO Med(MedID) VALUES ('"+dataObj[i].MedID+"')";
tx.executeSql(sql);
The correct way to give parameters to an SQL statement is as follows:
var sql = "INSERT INTO Med(MedID) VALUES (?)";
tx.executeSql(sql, [dataObj[i].MedID]);
It looks like you are missing the space that is needed between the table name and the column names.
Try this:
var sql = "INSERT INTO Med (MedID) VALUES ('"+dataObj[i].MedID+"')";
tx.executeSql(sql);
Make sure your dataObj[i].MedID is also defined. Add a console.log(sql) before your executeSql statement to check the command before using it.

Resources