python 笔记 之 链接数据库 创建表

'''
链接数据库
创建表
'''

import pymysql

# 创建连接
conn = pymysql.connect(host="192.168.4.1",port=1036,db="test",username="",password="")
# 创建游标
cus = conn.cursor()

sql = "select * from test;"
'''
# 执行
cus.execute(sql)
#取所有结果, 取结果之前
cus.fetchall()
#取一个结果
cus.fetchone()
#取10行数据
cus.fetchmany(size=10)
#提交
cus.commit()

# 关闭游标
cus.close()
cus.executemany()


'''
try:
    cus.execute(sql)
    result = cus.fetchone()
    print(result)
except Exception as e:
    raise e
finally:  # 这样就可以无论怎样就可以关闭
    cus.close()
    conn.close()

'''
update
insert
create table table_name (id int not null);
create table %s (%s)
[(a1, id int not null) , (b2, name string) ]
字符串的format形式是类似的
'''

'''
id name age
a = Student(id=, name=, age=)
a.save()
insert
filter(id=100)
'''

'''
create table 表名(
  列名 数据类型 not null
);

create table Student(
  StdID int not null,
  StdName varchar(100),
  Gender enum('M','F'),
  Age tinyint
);

'123'         varchar(10)
'123      '   char(10)

'''
'''
show databases;  
show tables;
show variables like '%auto%';
show full processlist
# 授权超级权限
grant all privileges on *.* to
'username'@'%' identified by "123456"
with grant option;

Skip-grant-talbes

'''

猜你喜欢

转载自my.oschina.net/u/3824134/blog/1809211