My Sounds table has 7 columns: Start_Time
, End_Time
, Salience_Label
, Class_label
, Bitrate
, Bitdepth
, Samplerate
.
I want to insert some values into this table with the command
cursor.execute("INSERT INTO Sounds VALUES (%s, %s, %s, %s, %s, %s, %s)",(start_time, end_time, salience_label, class_label, samplerate, bitrate, bitdepth))
try:
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
.... doing staff for getting values for my variables ...
cursor.execute("INSERT INTO Sounds VALUES (%s, %s, %s, %s, %s, %s, %s)",(start_time, end_time, salience_label, class_label, samplerate, bitrate, bitdepth))
print "Connected!\n"
except:
print "I am unable to connect to the database"
cursor.close()
conn.close()
print('Close conection')
While testing do not catch exceptions. Make the parameters a single tuple as Psycopg will adapt it to a record. Use mogrify
to check what is being sent to the server:
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
insert = "insert into Sounds values %s"
parameters = (
start_time, end_time, salience_label,
class_label, samplerate, bitrate, bitdepth
)
print cursor.mogrify(insert, (parameters,))
cursor.execute(insert, (parameters,))
conn.commit()
cursor.close()
conn.close()
BTW, the good practice is to name the columns which will receive the data like in:
insert into t (col_a, col_b) values (1, 'a')
That will avoid some problems.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.