1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import mysql.connector from mysql.connector.cursor import MySQLCursorPrepared from datetime import datetime from datetime import date import my_sql_select
def main(config): conn = mysql.connector.Connect(**config) cour = conn.cursor(cursor_class=MySQLCursorPrepared) prepstmt = "insert into student (name, age, brd, CREATEDT) values (%s, %s, %s, %s)" cour.execute(prepstmt) created = datetime.now() cour.execute(prepstmt, ("test", 29, date(1988, 3, 27), created)) conn.commit()
if __name__ == '__main__': config = { 'host': 'localhost', 'port': 3306, 'database': 'test', 'user': 'root', 'password': '!qaz2wsx', 'charset': 'utf8', 'use_unicode': True, 'get_warnings': True, } print("----------before insert------------") output = my_sql_select.queryAllStu(config) print('\n'.join(output)) main(config) print("----------after insert------------") output = my_sql_select.queryAllStu(config) print('\n'.join(output))
|