Skip to content

Commit 3d7e1b1

Browse files
committed
add readdb.py function
1 parent aad6926 commit 3d7e1b1

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

‎306.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,28 @@ render()方法能够告诉tornado读入哪个模板,插入其中的模板代
2626
one_user = usernames[0][0]
2727
self.render("index.html", user=one_user)
2828

29-
index.py文件中,只修改了get()方法,从数据库中读取用户名,并且提出一个用户(one_user),然后通过`self.render("index.html", user=one_user)`将这个用户名放到index.html中,其中`user=one_user`的作用就是传递对象到模板。index.html的代码修改为:
29+
index.py文件中,只修改了get()方法,从数据库中读取用户名,并且提出一个用户(one_user),然后通过`self.render("index.html", user=one_user)`将这个用户名放到index.html中,其中`user=one_user`的作用就是传递对象到模板。
30+
31+
提醒读者注意的是,在上面的代码中,我使用了`mrd.select_columns(table="users",column="username")`,也就是说必须要在methods目录中的readdb.py文件中有一个名为select_columns的函数。为了使读者能够理解,贴出已经修改之后的readdb.py文件代码,比上一节多了函数select_columns:
32+
33+
#!/usr/bin/env python
34+
# coding=utf-8
35+
36+
from db import *
37+
38+
def select_table(table, column, condition, value ):
39+
sql = "select " + column + " from " + table + " where " + condition + "='" + value + "'"
40+
cur.execute(sql)
41+
lines = cur.fetchall()
42+
return lines
43+
44+
def select_columns(table, column ):
45+
sql = "select " + column + " from " + table
46+
cur.execute(sql)
47+
lines = cur.fetchall()
48+
return lines
49+
50+
下面是index.html修改后的代码:
3051

3152
<!DOCTYPE html>
3253
<head>

0 commit comments

Comments
 (0)