user9740954
user9740954

Reputation:

Why is my INSERT INTO sql query not working?

I have this line:

result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
                        username=request.form.get("username"), hash=hash)

that when executes, fails to insert anything into my database. I'm honestly so confused as to why this doesn't work, and I even copied over a SQL query from a previous program I made just to make sure. I'm not getting any "error" but on the terminal, the line is yellow instead of green, which it usually is when the query is successful.

My db looks like this:

To be specific, my desired result is to insert both the username and hash into my database.

Upvotes: 1

Views: 412

Answers (2)

ASH
ASH

Reputation: 20352

This is how I would do it.

import pyodbc
user='sa'
password='PSWD'
database='NORTHWND'
port='1433'
TDS_Version='8.0'
server='xxx.xxx.xxx.xxx'
driver='FreeTDS'

   con_string='UID=%s;PWD=%s;DATABASE=%s;PORT=%s;TDS=%s;SERVER=%s;driver=%s' % (user,password, database,port,TDS_Version,server,driver)
   cnxn=pyodbc.connect(con_string)
   cursor=cnxn.cursor()
   cursor.execute("INSERT INTO mytable(name,address) VALUES (?,?)",('thavasi','mumbai'))
   cnxn.commit() 

Upvotes: 0

JVella94
JVella94

Reputation: 48

From my (uninformed) point of view, you're trying to insert a new row with only 2 column values. Yet the other column values cannot be null.

So insert the whole row?

Upvotes: 2

Related Questions