一. 数据库
1. 数据库连接:
def getdb_conn():
try:
pgdb_conn = psycopg2.connect("host=localhost dbname=tdnsla user=phppgadmin")
pg_cursor = pgdb_conn.cursor()
except Exception, e:
print e
return "",""
return pgdb_conn,pg_cursor
- 数据库插入:
def runsql(pgdb_conn, pg_cursor, sql):
try:
pg_cursor.execute(sql)
pgdb_conn.commit()
except Exception, e:
print e
- 数据库查找
def runSelect(pgdb_conn, pg_cursor, sql):
pg_cursor.execute(sql)
s = []
for row in pg_cursor.fetchall():
s.append(row)
return s
二. 执行shell命令
def runCmd(cmd):
returnCode = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print returnCode.stdout.read()