import pymysql
class MyOrm():
def init(self):
conn = pymysql.connect(
host=“192.168.124.9”,
port=3306,
user=“admin2”,
password=“123”,
database=“mumushouji”,
charset=“utf8”
)
cursor = conn.cursor()
self.cursor = cursor
def execute(self, sql):
self.cursor.execute(sql)
return self.cursor.fetchall()
class Comment:
table_name = "comment"
def init(self, **kwargs):
for k, v in kwargs.items():
self.setattr(k, v)
def insert(self):
keys = []
values = []
for k, v in self.dict.items():
keys.append(k)
values.append(v)
sql = “insert into %s(%s) values(’%s’)” % (self.table_name, ‘,’.join(keys), “’,’”.join(values))
print(sql)
return MyOrm().execute(sql)
if name == “main”:
comment = Comment(user_id=“1”,
article_id=“2”,
ipaddr=“127.0.0.1”
)
comment.insert()
插入后数据库中不显示数据,但是用生成的sql语句查询确能够成功插入并显示数据。
可以看到自增的primarykey id从2跳到4,跳了一个说明插入进去了但是不显示