Home python Filling a table in MS SQL via Python

Filling a table in MS SQL via Python

Author

Date

Category

created a program that should fill the table with random values ​​(for tests). Manually completed table and data types are on the attached image.
Here is the code:

import random
Import Pypyodbc.
mysqlserver = "Desktop-1FN3B94 \ SQLEXPRESS"
MyDatabase = "Coachs"
Try:
  Connection = Pypyodbc.connect ("Driver = {SQL Server};"
                 "Server =" + mysqlserver + ";"
                 "Database =" + MyDatabase + ";")
Except:
  Print ("Error connecting to the database")
Cursor = Connection.cursor ()
Names = ['Abakum', 'Abram', 'Abrosim' ...]
Sure_Names = ['Ababkov', 'Abakumov', 'Abalyshev', ...]
Exams = ['Ogue,', 'Ege,', 'Ogue, Ege,']
SUBS = ['Mathematics', 'Russian Language', ...]
Areas = ['Dzerzhinsky', 'Industrial', 'Kirovsky', ...]
Streets = [Lenin ',' Wagon ',' Pushkin ', ...]
For i in Range (3):
  Age = Random.RANDRANGE (20, 70)
  Like = Random.randrange (25, 100)
  dislike = random.randrange (2, 10)
  Number = Random.RANDRANGE (89012303027, 89090000000)
  Price = random.randrange (400, 851)
  Name = Random.Choice (Names) + "" + Random.Choice (Sure_Names)
  Area = Random.Choice (Areas) + ", All"
  Sub = Random.Choice (SUBS)
  Exam = Random.Choice (Exams)
  address_num = random.randrange (5, 105)
  Address = Random.Choice (Streets) + "," + str (address_num)
  mysqlquery = ("INSERT INTO DBO.TUTOR_INFO VALUES (?,? ,?,?,? ,? ,?,
  ?,?,?,?) ", (Name, I + 6, Str (Number), Age, Area, Exam, Sub, Like,
  dislike, Price, Address))
  CURSOR.EXECUTE (MYSQLQUERY)
Connection.Close ()

and the error he gives out such:

Traceback (Most Recent Call Last):
 File "C: /Ussers/Endeller/.pycharmace2019.2/config/scratches/base / test.py", Line 41, in & lt; module & gt;
  CURSOR.EXECUTE (MYSQLQUERY)
 File "C: \ Users \ Owner \ AppData \ Local \ Programs \ Python \ Python37 \ Lib \ Site-Packages \ Pypyodbc.py", Line 1626, In Execute
  Self.Execdirect (Query_String)
 File "C: \ Users \ Owner \ APPDATA \ LOCAL \ PROGRAMS \ PYTHON \ PYTHON37 \ LIB \ Site-Packages \ Pypyodbc.py", Line 1650, In Execdirect
  c_query_string = ctypes.c_char_p (query_string)
TypeRor: Bytes or Integer Address Expected Instad Of Tuple Instance
Process Finished With Exit Code 1


Answer 1, Authority 100%

cursor.execute () expects a string with a SQL query as the first argument. You handed it a tuple (Tuple ).

Try this:

mysqlquery = "insert into dbo.tutor_info values ​​(?,? ,? ,? ,?,? ,? ,?, ?,?,?) "
PARMS = (Name, I + 6, Str (Number), Age, Area, Exam, Sub, Like, Dislike, Price, Address)
Cursor.Execute (MysqlQuery, Parms)

Do not forget to make Commit at the end:

connection.commit ()
Connection.Close ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions