python学习

python学习

笔记一:

json常见用法-loads、jumps、load、jump

json.dumps 将 Python 对象编码成 JSON 字符串

json.loads 将已编码的 JSON 字符串解码为 Python 对象

json.dump和json.load,需要传入文件描述符,加上文件操作。

JSON内部的格式要注意,一个好的格式能够方便读取,可以用indent格式化。


笔记二:

1.assertEqual(self, first, second, msg=None)

--判断两个参数相等:first == second

2.assertNotEqual(self, first, second, msg=None)

--判断两个参数不相等:first != second

3.assertIn(self, member, container, msg=None)

--判断是字符串是否包含:member in container

4.assertNotIn(self, member, container, msg=None)

--判断是字符串是否不包含:member not in container

5.assertTrue(self, expr, msg=None)

--判断是否为真:expr is True

6.assertFalse(self, expr, msg=None)

--判断是否为假:expr is False

7.assertIsNone(self, obj, msg=None)

--判断是否为 None:obj is None

8.assertIsNotNone(self, obj, msg=None)

--判断是否不为 None:obj is not None


笔记三:

python如何连接数据库并进行操作

https://www.cnblogs.com/du-hong/p/10897822.html

pyton连接数据库需要先安装pymysql模块:pip install pymysql

安装完成后导入pymysql模块:import pymysql

python连接数据库主要分五个步骤:

step1:连接数据库

step2:创建游标对象

step3:对数据库进行增删改查

step4:关闭游标

step5:关闭连接

笔记四:

    1、建立数据库连接

     import MySQLdb

     conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable")

    cursor=conn.cursor()

    2、执行数据库操作

     n=cursor.execute(sql,param)

     我们要使用连接对象获得一个cursor对象,接下来,我们会使用cursor提供的方法来进行工作.

     这些方法包括两大类:1.执行命令,2.接收返回值

    3、cursor用来执行命令的方法:

     callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数

     execute(self, query, args):执行单条sql语句,接收的参数为sql语句本身和使用的参数列表,返回值为受影响的行数

     executemany(self, query, args):执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数

     nextset(self):移动到下一个结果集

    4、cursor用来接收返回值的方法:

     fetchall(self):接收全部的返回结果行.

     fetchmany(self, size=None):接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.

     fetchone(self):返回一条结果行.

     scroll(self, value, mode='relative'):移动指针到某一行.如果mode='relative',则表示从当前所在行移动value条,如果mode='absolute',则表示从结果集的第一 行移动value条.


import pymysql

import time

from config.config import conf_parse

class connect_mysql:

    def __init__(self):

        conf = conf_parse()

        host = conf.get("client", "ip")

        user = conf.get("client", "mysql_usr")

        pwd = conf.get("client", "mysql_pwd")

        port = conf.get("client", "mysql_port")

        database_name = conf.get("client", "database_name")

        self.db = pymysql.connect(host = host, port = port,user = user, password = pwd, db = database_name,autocommit=True)

        self.cursor = self.db.cursor()

    def truncate_db(self, table_name):

        cursor = self.cursor

        for i in table_name:

            truncate_db = cursor.execute("truncate table %s" % i)

            print(f"truncate db {i} done --")

            time.sleep(1)

        return truncate_db

    def select_element(self, params, table_name, condition):

        cursor = self.cursor

        if condition == '':

            cursor.execute("select {} from {}".format(params, table_name))执行单条语句

        else:

            cursor.execute("select {} from {} where {}".format(params, table_name, condition))

        select_element = cursor.fetchall()

        return select_element

    def update_db(self,table_name,option,condition):

        cursor = self.cursor

        db = self.db

        cursor.execute("update {} set {} where {}".format(table_name,option,condition))

        db.commit()

    def insert(self,sql,argus_list=None):

        cursor = self.cursor

        db = self.db

        cursor.execute(sql,argus_list)

        db.commit()

    def close(self):关闭游标,关闭数据库

        self.cursor.close()

        self.db.close()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。