重庆思庄Oracle、Redhat认证学习论坛

标题: 使用PYTHON往 PG中插入数据 [打印本页]

作者: 郑全    时间: 2023-12-10 18:49
标题: 使用PYTHON往 PG中插入数据
1.SQL语句
   [postgres@eulersvr ~]$ cat insert-row.py
   # _*_ coding:utf-8 _*_
   import psycopg2

   conn= psycopg2.connect(database="postgres",user="postgres",password="postgres",host="192.168.0.38",port="5432")

   cursor=conn.cursor()
   sql="""insert into emp(empno,last_name) values(%s,%s)"""
   params = (100,'mahan')

   cursor.execute(sql,params)

   print ("successfully")

   conn.commit()
   conn.close()


2.执行SQL

  [postgres@eulersvr ~]$ python3 insert-row.py
successfully

3.验证

  [postgres@eulersvr ~]$ psql
psql (16.1)
Type "help" for help.

postgres=# select * from emp;
id | empno | last_name
----+-------+-----------
  1 |   100 | mahan
(1 row)

postgres=#







欢迎光临 重庆思庄Oracle、Redhat认证学习论坛 (http://bbs.cqsztech.com/) Powered by Discuz! X3.2