├── example.py ├── README.md ├── Mysql.class.php └── libmysql.py /example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | """ table structure; 5 | CREATE TABLE `users` ( 6 | `id` int(11) NOT NULL AUTO_INCREMENT, 7 | `email` varchar(255) COLLATE utf8_bin NOT NULL, 8 | `password` varchar(255) COLLATE utf8_bin NOT NULL, 9 | PRIMARY KEY (`id`) 10 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 11 | """ 12 | 13 | from libmysql import MYSQL 14 | 15 | # msyql dababase connection info 16 | dbconn = MYSQL( 17 | dbhost = 'localhost', 18 | dbuser = 'root', 19 | dbpwd = '', 20 | dbname = 'wyproxy', 21 | dbcharset = 'utf8') 22 | 23 | # insert data, 插入数据 24 | user = {'email': 'ringzero@0x557.org', 'password': '123123'} 25 | dbconn.insert(table='users', data=user) 26 | 27 | # change user dict, 修改用户信息提交 28 | user['email'] = 'ringzero@wooyun.org' 29 | user['password'] = '123456' 30 | dbconn.insert(table='users', data=user) 31 | 32 | # update 更新用户信息 33 | user = {'email': 'newringzero@0x557.org', 'password': '888888'} 34 | cond = {'email': 'ringzero@0x557.org'} 35 | rows = dbconn.update(table='users', data=user, condition=cond) 36 | print('update {} records success..'.format(rows)) 37 | 38 | # delete data, 删除数据, limit参数为删除少条 39 | cond = {'email': 'ringzero@0x557.org'} 40 | rows = dbconn.delete(table='users', condition=cond, limit='1') 41 | print('deleted {} records success..'.format(rows)) 42 | 43 | # 统计数据库记录条数 44 | cond = {'email': 'ringzero@wooyun.org'} 45 | cnt = dbconn.count( 46 | table='users', 47 | condition=cond) 48 | print(cnt) 49 | 50 | # select 查询信息 51 | fields = ('id', 'email') 52 | cond = {'email': 'ringzero@wooyun.org'} 53 | rows = dbconn.fetch_rows( 54 | table='users', 55 | fields=fields, 56 | condition=cond, 57 | order='id asc', 58 | limit='0,5') 59 | 60 | for row in rows: 61 | print(row) 62 | 63 | # 不指定 fields 字段, 将返回所有*字段, 64 | # 不指定 order, 将不进行排序 65 | # 不指定 limit, 将返回所有记录 66 | 67 | rows = dbconn.fetch_rows( 68 | table='users', 69 | condition=cond, 70 | limit='0,5') 71 | for row in rows: 72 | print(row) 73 | 74 | # query 执行自定义SQL语句 75 | sql = 'select * from users limit 0, 5' 76 | rows = dbconn.query(sql) 77 | for row in rows: 78 | print(row) 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A Friendly pymysql CURD Class 2 | Based on Mysql.class.php 3 | 4 | [wyproxy](https://github.com/ring04h/wyproxy) 的web控制台, flask + pymysql 5 | 6 | 不想使用sqlalchemy, 又没有好用的轮子类, 就自己造一个了 7 | 8 | ## EXAMPLE 使用帮助 9 | ### 防止SQL注入建议 10 | 一定要记得针对来自客户端的变量进行安全转义 11 | ```python 12 | from pymysql import escape_string 13 | id = request.args.get('id') 14 | id = escape_string(id) 15 | ``` 16 | 17 | ### 使用参数绑定的方式来防止SQL注入 18 | ```python 19 | def insert(self, table, data): 20 | """mysql insert() function""" 21 | with self.connection.cursor() as cursor: 22 | params = self.join_field_value(data); 23 | sql = "INSERT INTO {table} SET {params}".format(table=table, params=params) 24 | cursor.execute(sql, tuple(data.values())) 25 | self.connection.commit() 26 | 27 | def join_field_value(self, data, glue = ', '): 28 | sql = comma = '' 29 | for key, value in data.items(): 30 | sql += "{}`{}` = %s".format(comma, key) 31 | comma = glue 32 | return sql 33 | ``` 34 | 35 | ### 引入Class类 36 | ```python 37 | from libmysql import MYSQL 38 | ``` 39 | 40 | ### 数据库表结构 41 | 42 | ```sql 43 | CREATE TABLE `users` ( 44 | `id` int(11) NOT NULL AUTO_INCREMENT, 45 | `email` varchar(255) COLLATE utf8_bin NOT NULL, 46 | `password` varchar(255) COLLATE utf8_bin NOT NULL, 47 | PRIMARY KEY (`id`) 48 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 49 | ``` 50 | 51 | ### 初始化数据库连接 52 | ```python 53 | # msyql dababase connection info 54 | dbconn = MYSQL( 55 | dbhost = 'localhost', 56 | dbuser = 'root', 57 | dbpwd = '', 58 | dbname = 'wyproxy', 59 | dbcharset = 'utf8') 60 | ``` 61 | 62 | ### 插入数据 63 | ```python 64 | user = {'email': 'ringzero@0x557.org', 'password': '123123'} 65 | dbconn.insert(table='users', data=user) 66 | 67 | # change user dict, 修改用户信息提交 68 | user['email'] = 'ringzero@wooyun.org' 69 | user['password'] = '123456' 70 | dbconn.insert(table='users', data=user) 71 | ``` 72 | 73 | ### update 更新信息 74 | ```python 75 | user = {'email': 'newringzero@0x557.org', 'password': '888888'} 76 | cond = {'email': 'ringzero@0x557.org'} 77 | rows = dbconn.update(table='users', data=user, condition=cond) 78 | print('update {} records success..'.format(rows)) 79 | ``` 80 | 81 | ### delete data, 删除数据, limit参数为删除少条 82 | ```python 83 | cond = {'email': 'ringzero@0x557.org'} 84 | rows = dbconn.delete(table='users', condition=cond, limit='1') 85 | print('deleted {} records success..'.format(rows)) 86 | ``` 87 | 88 | ### select 查询信息 89 | ```python 90 | fields = ('id', 'email') 91 | cond = {'email': 'ringzero@wooyun.org'} 92 | rows = dbconn.fetch_rows( 93 | table='users', 94 | fields=fields, 95 | condition=cond, 96 | order='id asc', 97 | limit='0,5') 98 | 99 | for row in rows: 100 | print(row) 101 | 102 | # 不指定 fields 字段, 将返回所有*字段, 103 | # 不指定 order, 将不进行排序 104 | # 不指定 limit, 将返回所有记录 105 | 106 | rows = dbconn.fetch_rows( 107 | table='users', 108 | condition=cond, 109 | limit='0,5') 110 | for row in rows: 111 | print(row) 112 | ``` 113 | 114 | ### 统计数据库记录条数 115 | 不指定 condition 字段, 将返回数据库的总记录条数 116 | ```python 117 | cond = {'email': 'ringzero@wooyun.org'} 118 | cnt = dbconn.count( 119 | table='users', 120 | condition=cond) 121 | print(cnt) 122 | ``` 123 | 124 | ### query 执行自定义SQL语句 125 | ```python 126 | sql = 'select * from users limit 0, 5' 127 | rows = dbconn.query(sql) 128 | for row in rows: 129 | print(row) 130 | ``` -------------------------------------------------------------------------------- /Mysql.class.php: -------------------------------------------------------------------------------- 1 | 'change the title', 13 | 'detail' => 'change the detail', 14 | 'begin_time' => '1'); 15 | 16 | $condition = array('id' => '71', 'title' => 'dsad'); 17 | $db->update('news', $data, $condition); 18 | 19 | 20 | // insert 21 | $data = array( 22 | 'id' => NULL, 23 | 'title' => '74', 24 | 'detail' => 'dsad', 25 | 'begin_time' => '0'); 26 | $db->insert('news', $data); 27 | 28 | // delete 29 | $condition = array( 30 | 'id' => '74', 31 | 'title' => 'dsad'); 32 | $db->delete('news', $condition); 33 | 34 | // query 35 | $query = $db->query('select * from u_members where uid < 100'); 36 | while ($members = $db->fetch_array($query)) { 37 | print_r($members); 38 | } 39 | */ 40 | 41 | class db_mysql { 42 | 43 | private $dbhost; 44 | private $dbuser; 45 | private $dbpwd; 46 | private $dbname; 47 | private $dbcharset; 48 | 49 | function __construct($dbhost = DB_HOST, $dbuser = DB_USER, $dbpw = DB_PASSWORD, $dbcharset = DB_CHARSET, $dbname = DB_NAME){ 50 | $this->dbhost = $dbhost; 51 | $this->dbuser = $dbuser; 52 | $this->dbpw = $dbpw; 53 | $this->dbcharset = $dbcharset; 54 | $this->dbname = $dbname; 55 | $this->connect(); 56 | } 57 | 58 | function connect(){ 59 | $link = @mysql_connect ( $this->dbhost, $this->dbuser, $this->dbpw ) or $this->halt( mysql_error() ); 60 | mysql_select_db( $this->dbname, $link ) or $this->halt ( mysql_error() ); 61 | mysql_query("SET NAMES '$this->dbcharset'"); 62 | } 63 | 64 | function delete($table, $condition, $limit = 0) { 65 | if (empty ( $condition )) { 66 | $where = '1'; 67 | } elseif (is_array ( $condition )) { 68 | $where = $this->implode_field_value ( $condition, ' AND ' ); 69 | } else { 70 | $where = $condition; 71 | } 72 | $sql = "DELETE FROM " . $table . " WHERE $where " . ($limit ? "LIMIT $limit" : ''); 73 | return $this->query ( $sql ); 74 | } 75 | 76 | function insert($table, $data, $return_insert_id = false) { 77 | $sql = $this->implode_field_value ( $data ); 78 | $return = $this->query ( "INSERT INTO $table SET $sql" ); 79 | return $return_insert_id ? $this->insert_id () : $return; 80 | } 81 | 82 | function update($table, $data, $condition) { 83 | $sql = $this->implode_field_value ( $data ); 84 | $where = ''; 85 | if (empty ( $condition )) { 86 | $where = '1'; 87 | } elseif (is_array ( $condition )) { 88 | $where = $this->implode_field_value ( $condition, ' AND ' ); 89 | } else { 90 | $where = $condition; 91 | } 92 | $res = $this->query ( "UPDATE $table SET $sql WHERE $where" ); 93 | return $res; 94 | } 95 | 96 | function fetch_array($query, $result_type = MYSQL_ASSOC) { 97 | return mysql_fetch_array ( $query, $result_type ); 98 | } 99 | 100 | function fetch_first($sql) { 101 | return $this->fetch_array ( $this->query ( $sql ) ); 102 | } 103 | 104 | function result_first($sql) { 105 | return $this->result ( $this->query ( $sql ), 0 ); 106 | } 107 | 108 | function affected_rows() { 109 | return mysql_affected_rows(); 110 | } 111 | 112 | function result($query, $row = 0) { 113 | $query = @mysql_result ( $query, $row ); 114 | return $query; 115 | } 116 | 117 | function num_rows($query) { 118 | $query = mysql_num_rows ( $query ); 119 | return $query; 120 | } 121 | 122 | function num_fields($query) { 123 | return mysql_num_fields ( $query ); 124 | } 125 | 126 | function free_result($query) { 127 | return mysql_free_result ( $query ); 128 | } 129 | 130 | function fetch_row($query) { 131 | $query = mysql_fetch_row ( $query ); 132 | return $query; 133 | } 134 | 135 | function fetch_fields($query) { 136 | return mysql_fetch_field ( $query ); 137 | } 138 | 139 | function version() { 140 | return mysql_get_server_info(); 141 | } 142 | 143 | function close() { 144 | return mysql_close(); 145 | } 146 | 147 | function implode_field_value($array, $glue = ',') { 148 | $sql = $comma = ''; 149 | foreach ($array as $k => $v) { 150 | $sql .= $comma."`$k`='$v'"; 151 | $comma = $glue; 152 | } 153 | return $sql; 154 | } 155 | 156 | public function halt($message = '', $sql = '') { 157 | echo "
$message