0

I have a python script that should be able to write data into a made SQL table. I have these lines that should do it:

#Connect to the database
testDBCon = sqlalchemy.create_engine('mssql+pyodbc://SRVISCDB,58837/testDB?driver=SQL+Server+Native+Client+11.0')

#Choose what query to select a column from
query = "SELECT * FROM testDB.dbo.SuspensiaImageDetails;"

query = "INSERT INTO testDB.dbo.SuspensiaImageDetails (FileName, 
         FileSize, FilePath, Height, Width, Format, Mode, Frames)VALUES ("
query += line
query += ");"

The output that I get when I print(query) and I get this:

    INSERT INTO testDB.dbo.SuspensiaImageDetails (FileName, FileSize, 
    FilePath, Height, Width, Format, Mode, Frames)VALUES (,X01LA0295.JPG
                      ,9718                           ,.JPG                           
       ,400
       600                            ,JPEG                           ,RGB                            
    ,1                              ,);

A few issues I see is all the gaps in-between the values, as well as commas in-between the values. I have this line that puts a comma in-between the values but it puts it in the front and end of every value. However those may be an issue as well.

line += f",{str(value):<30} "

My question is how can I fix these lines to get the values I have into the SQL database table?

8
  • 2
    Well for starters, you need single quotes around your char/varchar values. You've also got too many commas. Commented Jul 20, 2022 at 14:24
  • 1
    So I need a sing quote around each value I want to add? I added that by doing line += f",'{str(value):<30}' " Wat else would I need to do to add it to the Table Commented Jul 20, 2022 at 14:25
  • 2
    don't build the sql query by concat-ing together from Python strings, the db library has tools to help you do this safely and to automatically quote the values when needed according to the db backend and column type. See stackoverflow.com/a/32333755/202168 Commented Jul 20, 2022 at 14:30
  • 1
    After reading that i see that they use UPDATE i assume I would be using INSERT yet I am still confused on how the line would look Commented Jul 20, 2022 at 14:33
  • 2
    why do you use <30? it adds spaces to inserted strings. Simply use f"'{value}'" with ' ' inside " ". But it could be better to use special functions to create queries - like in some modules execute("INSERT ... (?, ?, ?, ...)", data) with ? or `% as placeholders Commented Jul 20, 2022 at 14:54

1 Answer 1

1

As mentioned by @furas, the best way would be to use placeholders. (%s for strings, %i for integers)

import sqlalchemy

testDBCon = sqlalchemy.create_engine('mssql+pyodbc://SRVISCDB,58837/testDB?driver=SQL+Server+Native+Client+11.0')

#Choose what query to select a column from
query1 = "SELECT * FROM testDB.dbo.SuspensiaImageDetails;"

query2 = "INSERT INTO testDB.dbo.SuspensiaImageDetails (FileName, 
         FileSize, FilePath, Height, Width, Format, Mode, Frames) VALUES (%s,%i,%s,%i,%i,%s,%s,%i)"

values = ('X01LA0295.JPG',9718,'.JPG',400,600,'JPEG','RGB',1) 

engine.execute(query2,values)    

Happy Coding!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.