Python Forum
Error creating database with python and form?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error creating database with python and form?
#1
I pass the database name from form to the cdb.py it is successful, but the mariadb query "CREATE DATABASE ?" cannot get the value i think so, this is cdb.py
# Get Cursor
cur = conn.cursor()
# creating database 
cur.execute("CREATE DATABASE ?", (val,)) 
  
cur.execute("SHOW DATABASES")
databaseList = cur.fetchall()
  
for database in databaseList:
  print(database)
    
conn.close()

when i run the cdb.py directly from command line this is the error:
<!-- The above is a description of an error in a Python program, formatted
     for a Web browser because the 'cgitb' module was enabled.  In case you
     are not reading this in a Web browser, here is the original traceback:

Traceback (most recent call last):
  File "/srv/http/cgi-bin/./cdb.py", line 31, in &lt;module&gt;
    cur.execute("CREATE DATABASE ?", (val,))
mariadb.ProgrammingError: 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 '?' at line 1

-->
This is the error printing in web browser when i run from the web browser:
 /srv/http/cgi-bin/cdb.py in <module>
     29 cur = conn.cursor()
     30 # creating database 
=>   31 cur.execute("CREATE DATABASE ?", (val,)) 
     32   
     33 cur.execute("SHOW DATABASES")
cur = <mariadb.connection.cursor object>, cur.execute = <built-in method execute of mariadb.connection.cursor object>, val = 'mydb'

ProgrammingError: 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 '?' at line 1
      args = ("You have an error in your SQL syntax; check the ...on for the right syntax to use near '?' at line 1",)
      errmsg = "You have an error in your SQL syntax; check the ...on for the right syntax to use near '?' at line 1"
      errno = 1064
      msg = "You have an error in your SQL syntax; check the ...on for the right syntax to use near '?' at line 1"
      sqlstate = '42000'
      with_traceback = <built-in method with_traceback of ProgrammingError object> 
Reply
#2
Hi @shams,
I have no MariaDB so I cannot check it but as far as I know the use of placeholders ( ? ) in a query is limited to DML statements. (DML = Data Manipulation Language, so only INSERT, UPDATE, DELETE).
So in the case of "Create database" you must provide the complete statement.
Reply
#3
I was trying something like this recently. I wanted to put the name of the db and weeknr both as %s in a SELECT command. No go.

This (val,) puts the string, the name of your db in the CREATE command, but it will have ordinary apostrophes, i.e. ' around it. This is not acceptable in SQL.

Gotta be those funny backstroke apostrophes!

I haven't found a way around this yet! If you find one, I'd be glad to hear it!

This worked for me

sql = f"SELECT studentnr, score FROM allstudentsAnswers{clas} WHERE weeknr = %s"
cur.execute(sql, (weeknr,))
Reply
#4
I know it is frowned upon, but sometimes you just have to revert to building the string yourself.
cur.execute(f"CREATE DATABASE {val}") 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,837 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  pyscript index error while calling input from html form pyscript_dude 2 973 May-21-2023, 08:17 AM
Last Post: snippsat
  Start Putty into Python Form Schlazen 5 5,458 Dec-13-2022, 06:28 AM
Last Post: divya130
  error 1102 (42000) incorrect database name 's' Anldra12 4 1,700 Jun-08-2022, 09:00 AM
Last Post: Anldra12
  I need help parsing through data and creating a database using beautiful soup username369 1 1,705 Sep-22-2021, 08:45 PM
Last Post: Larz60+
  mySQL Database error not resolving. cybertooth 2 3,196 Aug-30-2021, 05:45 PM
Last Post: ibreeden
  Error using mariadb select query with form in python? shams 2 1,997 Jul-29-2021, 12:30 PM
Last Post: shams
  html, python, a simple form test 1 1,936 Aug-09-2020, 01:59 PM
Last Post: snippsat
  Trying to send input from html form to database SQLite3 RonnyGiezen 3 67,775 Dec-21-2019, 02:09 PM
Last Post: ibreeden
  How to get first 5 images form the document using Python BeautifulSoup sarath_unrelax 0 1,636 Dec-19-2019, 07:13 AM
Last Post: sarath_unrelax

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020