python----pymysql

 pymysql存儲過程:java

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 執行存儲過程
cursor.callproc('p1', args=(1, 22, 3, 4))
# 獲取執行完存儲的參數
cursor.execute("select @_p1_0,@_p1_1,@_p1_2,@_p1_3")
result = cursor.fetchall()

conn.commit()
cursor.close()
conn.close()
print(result)

 

import pymysql

#添加數據

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='yyy')

cursor = conn.cursor()


# sql = """CREATE TABLE EMPLOYEE (
#          FIRST_NAME  CHAR(20) NOT NULL,
#          LAST_NAME  CHAR(20),
#          AGE INT,
#          SEX CHAR(1),
#          INCOME FLOAT )"""
# 
# cursor.execute(sql)

#row_affected = cursor.execute("create table t1(id INT ,name VARCHAR(20))")

#row_affected=cursor.execute("INSERT INTO t1(id,name) values (1,'alvin'),(2,'xialv')")

#cursor.execute("update t1 set name = 'silv2' where id=2")




#查詢數據
row_affected=cursor.execute("select * from t1")
one=cursor.fetchone()

# many=cursor.fetchmany(2)
# all=cursor.fetchall()



#scroll
#cursor.scroll(-1,mode='relative')  # 相對當前位置移動

#cursor.scroll(2,mode='absolute') # 相對絕對位置移動


#更改獲取數據結果的數據類型,默認是元組,能夠改成字典等:conn.cursor(cursor=pymysql.cursors.DictCursor)


conn.commit()
cursor.close()
conn.close()
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息