CREATE USER `root`@`127.0.0.1` IDENTIFIED BY 'passowrd';
GRANT ALL ON *.* TO `root`@`127.0.0.1` WITH GRANT OPTION;
insert into tablename(col1,col2) values ('%s','%d') % (val1,val2)
insert into tablename(col1,col2) values ('1','2')
create table house(
name varchar(200) NOT NULL,
area varchar(200),
address varchar(200),
type_how_big varchar(100),
type_howmany_room varchar(500),
type_which_floor varchar(100),
type_direction varchar(100),
type_time varchar(100),
price varchar(100),
junjia varchar(500),
city varchar(50),
page integer
);
truncate 清洗表数据,但保留表结构
drop 删除表,不可回滚
delete 删除表,可回滚
SELECT COUNT(*) FROM table_name;
import pymysql
def dbHandle():
try:
conn = pymysql.connect(
host="localhost",# 主机名
db="sofang",# 数据库名
port=3306,
user="root",
passwd="root",
charset="utf8",
use_unicode=True,
)
except OperationalError as e:
print e
return conn
def select(self,table_name):
#sql语句之间应该有空格
sql = 'select * from '+table_name+';'
cursor = self.conn.cursor() #创建游标
cursor.execute(sql) #执行sql
self.conn.commit() #提交
#将查询的数据打印出来
results = cursor.fetchall()
for row in results:
print row
def insert_inspection_list(self,table_name):
for i in range(1,100):
id = str(i)
inspection_num = 'NJ'+ str(100000+i)
car_id = i+1
create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
update_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
#@@@@@字符串用双引号加单引号"''",三种sql语句
sql1 = 'insert into car (id,inspection_num,car_id, create_uid) values
('+id+',"' + inspection_num + '",' + car_id + ',238);
sql2 = 'insert into car (id,inspection_num,car_id,create_uid,create_time,
update_time)values({},{},{},{},{},{})'.format(id,inspection_num,car_id,238,create_time,
update_time)
sql3 = 'insert into car (id,inspection_num,car_id,create_uid,create_time,
update_time)values (%s,%s,%d,%s,%s,%s)' % (id, inspection_num, car_id, 238, create_time,
update_time)
print sql
cursor = self.conn.cursor()
cursor.execute(sql)
self.conn.commit()
def close(self):
self.conn.close()