<span style="font-size: 18pt"><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">!/usr/bin/env python</span> <span style="color: #008080"> 2</span> <span style="color: #008000">#</span><span style="color: #008000"> -*- coding:utf-8 -*-</span> <span style="color: #008080"> 3</span> <span style="color: #008000">#</span><span style="color: #008000"> __author__ = "blzhu"</span> <span style="color: #008080"> 4</span> <span style="color: #800000">"""</span> <span style="color: #008080"> 5</span> <span style="color: #800000">python study </span><span style="color: #008080"> 6</span> <span style="color: #800000">Date:2017 </span><span style="color: #008080"> 7</span> <span style="color: #800000">"""</span> <span style="color: #008080"> 8</span> <span style="color: #008000">#</span><span style="color: #008000"> -*- coding: utf-8 -*-</span> <span style="color: #008080"> 9</span> <span style="color: #800080">__author__</span> = <span style="color: #800000">'</span><span style="color: #800000">djstava@gmail.com</span><span style="color: #800000">'</span> <span style="color: #008080">10</span> <span style="color: #008080">11</span> <span style="color: #0000ff">import</span><span style="color: #000000"> logging </span><span style="color: #008080">12</span> <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql </span><span style="color: #008080">13</span> <span style="color: #008080">14</span> <span style="color: #008080">15</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MySQLCommand(object): </span><span style="color: #008080">16</span> <span style="color: #0000ff">def</span> <span style="color: #800080">__init__</span><span style="color: #000000">(self, host, port, user, passwd, db, table, charset): </span><span style="color: #008080">17</span> self.host =<span style="color: #000000"> host </span><span style="color: #008080">18</span> self.port =<span style="color: #000000"> port </span><span style="color: #008080">19</span> self.user =<span style="color: #000000"> user </span><span style="color: #008080">20</span> self.password =<span style="color: #000000"> passwd </span><span style="color: #008080">21</span> self.db =<span style="color: #000000"> db </span><span style="color: #008080">22</span> self.table =<span style="color: #000000"> table </span><span style="color: #008080">23</span> self.charset =<span style="color: #000000"> charset </span><span style="color: #008080">24</span> <span style="color: #008080">25</span> <span style="color: #0000ff">def</span><span style="color: #000000"> connectMysql(self): </span><span style="color: #008080">26</span> <span style="color: #0000ff">try</span><span style="color: #000000">: </span><span style="color: #008080">27</span> self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=<span style="color: #000000">self.password, </span><span style="color: #008080">28</span> db=self.db, charset=<span style="color: #000000">self.charset) </span><span style="color: #008080">29</span> self.cursor =<span style="color: #000000"> self.conn.cursor() </span><span style="color: #008080">30</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">connect </span><span style="color: #800000">'</span> + self.table + <span style="color: #800000">'</span><span style="color: #800000"> correctly!</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">31</span> <span style="color: #0000ff">except</span><span style="color: #000000">: </span><span style="color: #008080">32</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">connect mysql error.</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">33</span> <span style="color: #008080">34</span> <span style="color: #0000ff">def</span><span style="color: #000000"> queryMysql(self): </span><span style="color: #008080">35</span> sql = <span style="color: #800000">"</span><span style="color: #800000">SELECT * FROM </span><span style="color: #800000">"</span> +<span style="color: #000000"> self.table </span><span style="color: #008080">36</span> <span style="color: #008080">37</span> <span style="color: #0000ff">try</span><span style="color: #000000">: </span><span style="color: #008080">38</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">query Mysql:</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #008080">39</span> <span style="color: #000000"> self.cursor.execute(sql) </span><span style="color: #008080">40</span> <span style="color: #008000">#</span><span style="color: #008000">row = self.cursor.fetchone()</span> <span style="color: #008080">41</span> <span style="color: #0000ff">for</span> d <span style="color: #0000ff">in</span><span style="color: #000000"> self.cursor: </span><span style="color: #008080">42</span> <span style="color: #0000ff">print</span>(str(d[0]), str(d[1]), str(d[2<span style="color: #000000">])) </span><span style="color: #008080">43</span> <span style="color: #008000">#</span><span style="color: #008000"> print(row)</span> <span style="color: #008080">44</span> <span style="color: #008080">45</span> <span style="color: #0000ff">except</span><span style="color: #000000">: </span><span style="color: #008080">46</span> <span style="color: #0000ff">print</span>(sql + <span style="color: #800000">'</span><span style="color: #800000"> execute failed.</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">47</span> <span style="color: #008080">48</span> <span style="color: #0000ff">def</span><span style="color: #000000"> insertMysql(self, id, name, sex): </span><span style="color: #008080">49</span> sql = <span style="color: #800000">"</span><span style="color: #800000">INSERT INTO </span><span style="color: #800000">"</span> + self.table + <span style="color: #800000">"</span><span style="color: #800000"> VALUES(</span><span style="color: #800000">"</span> + id + <span style="color: #800000">"</span><span style="color: #800000">,</span><span style="color: #800000">"</span> + <span style="color: #800000">"</span><span style="color: #800000">'</span><span style="color: #800000">"</span> + name + <span style="color: #800000">"</span><span style="color: #800000">',</span><span style="color: #800000">"</span> + <span style="color: #800000">"</span><span style="color: #800000">'</span><span style="color: #800000">"</span> + sex + <span style="color: #800000">"</span><span style="color: #800000">')</span><span style="color: #800000">"</span> <span style="color: #008080">50</span> <span style="color: #0000ff">try</span><span style="color: #000000">: </span><span style="color: #008080">51</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">insert Mysql:</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #008080">52</span> <span style="color: #000000"> self.cursor.execute(sql) </span><span style="color: #008080">53</span> <span style="color: #0000ff">print</span><span style="color: #000000">(sql) </span><span style="color: #008080">54</span> <span style="color: #0000ff">except</span><span style="color: #000000">: </span><span style="color: #008080">55</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">insert failed.</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #008080">56</span> <span style="color: #008080">57</span> <span style="color: #0000ff">def</span><span style="color: #000000"> updateMysqlSN(self, name, sex): </span><span style="color: #008080">58</span> sql = <span style="color: #800000">"</span><span style="color: #800000">UPDATE </span><span style="color: #800000">"</span> + self.table + <span style="color: #800000">"</span><span style="color: #800000"> SET sex='</span><span style="color: #800000">"</span> + sex + <span style="color: #800000">"</span><span style="color: #800000">'</span><span style="color: #800000">"</span> + <span style="color: #800000">"</span><span style="color: #800000"> WHERE name='</span><span style="color: #800000">"</span> + name + <span style="color: #800000">"</span><span style="color: #800000">'</span><span style="color: #800000">"</span> <span style="color: #008080">59</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">update sn:</span><span style="color: #800000">"</span> +<span style="color: #000000"> sql) </span><span style="color: #008080">60</span> <span style="color: #008080">61</span> <span style="color: #0000ff">try</span><span style="color: #000000">: </span><span style="color: #008080">62</span> <span style="color: #000000"> self.cursor.execute(sql) </span><span style="color: #008080">63</span> <span style="color: #000000"> self.conn.commit() </span><span style="color: #008080">64</span> <span style="color: #0000ff">except</span><span style="color: #000000">: </span><span style="color: #008080">65</span> <span style="color: #000000"> self.conn.rollback() </span><span style="color: #008080">66</span> <span style="color: #008080">67</span> <span style="color: #0000ff">def</span> deleteMysql(self, id): <span style="color: #008000">#</span><span style="color: #008000"> 删除</span> <span style="color: #008080">68</span> sql = <span style="color: #800000">"</span><span style="color: #800000">DELETE FROM %s WHERE id='%s'</span><span style="color: #800000">"</span> %<span style="color: #000000"> (self.table,id) </span><span style="color: #008080">69</span> <span style="color: #008000">#</span><span style="color: #008000">"delete from student where zid='%s'" % (id)</span> <span style="color: #008080">70</span> <span style="color: #0000ff">try</span><span style="color: #000000">: </span><span style="color: #008080">71</span> <span style="color: #000000"> self.cursor.execute(sql) </span><span style="color: #008080">72</span> <span style="color: #0000ff">print</span><span style="color: #000000">(sql) </span><span style="color: #008080">73</span> <span style="color: #000000"> self.conn.commit() </span><span style="color: #008080">74</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">delete the </span><span style="color: #800000">"</span> + id + <span style="color: #800000">"</span><span style="color: #800000">th row successfully!</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #008080">75</span> <span style="color: #0000ff">except</span><span style="color: #000000">: </span><span style="color: #008080">76</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">delete failed!</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #008080">77</span> <span style="color: #000000"> self.conn.rollback() </span><span style="color: #008080">78</span> <span style="color: #008080">79</span> <span style="color: #0000ff">def</span><span style="color: #000000"> closeMysql(self): </span><span style="color: #008080">80</span> self.conn.commit() <span style="color: #008000">#</span><span style="color: #008000"> 不执行此句,所作的操作不会写入到数据库中</span> <span style="color: #008080">81</span> <span style="color: #000000"> self.cursor.close() </span><span style="color: #008080">82</span> <span style="color: #000000"> self.conn.close() </span><span style="color: #008080">83</span> <span style="color: #008080">84</span> <span style="color: #008080">85</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">'</span><span style="color: #800000">__main__</span><span style="color: #800000">'</span><span style="color: #000000">: </span><span style="color: #008080">86</span> zblmysql = MySQLCommand(host=<span style="color: #800000">'</span><span style="color: #800000">localhost</span><span style="color: #800000">'</span>, user=<span style="color: #800000">'</span><span style="color: #800000">root</span><span style="color: #800000">'</span>, passwd=<span style="color: #800000">'</span><span style="color: #800000">root</span><span style="color: #800000">'</span>, db=<span style="color: #800000">'</span><span style="color: #800000">zbltest1</span><span style="color: #800000">'</span>, port=3306, table=<span style="color: #800000">'</span><span style="color: #800000">student2</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #008080">87</span> charset=<span style="color: #800000">'</span><span style="color: #800000">utf8</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">88</span> <span style="color: #000000"> zblmysql.connectMysql() </span><span style="color: #008080">89</span> <span style="color: #000000"> zblmysql.queryMysql() </span><span style="color: #008080">90</span> zblmysql.insertMysql(<span style="color: #800000">'</span><span style="color: #800000">5</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">zbl5</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">man</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">91</span> <span style="color: #000000"> zblmysql.queryMysql() </span><span style="color: #008080">92</span> zblmysql.deleteMysql(id=2<span style="color: #000000">) </span><span style="color: #008080">93</span> <span style="color: #000000"> zblmysql.queryMysql() </span><span style="color: #008080">94</span> zblmysql.updateMysqlSN(name=<span style="color: #800000">'</span><span style="color: #800000">zbl5</span><span style="color: #800000">'</span>,sex=<span style="color: #800000">'</span><span style="color: #800000">woman</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">95</span> <span style="color: #000000"> zblmysql.queryMysql() </span><span style="color: #008080">96</span> zblmysql.closeMysql()</span>
refer to:
A lot of fun!
The above is the detailed content of python3.4 uses functions to operate mysql5.7 database. For more information, please follow other related articles on the PHP Chinese website!