天天看点

三、软件数据库的应用前言一、数据库二、使用步骤

文章目录

  • 前言
  • 一、数据库
    • 1.基本格式
    • 2.读
    • 2.写
  • 二、使用步骤
    • 1.引入库
    • 2.读入数据

前言

数据库和thread

一、数据库

1.基本格式

import pymysql
if __name__ == '__main__':
    # 连接数据库,
    # 属性依次为:
    # 连接名称,端口,
    # 账号,密码,
    # 数据库的名字,字符集
    connection = pymysql.connect(host='', port=,
                                 user='', password='',
                                 db='', charset=')
    # 生成游标对象
    cur = connection.cursor()
    i=1
    print(type(i))
    while i<21:
        sql = "select * from petroleum where id in ('%s')" %(i)
        cur.execute(sql)
        # 通过fetchall方法获得数据
        data = cur.fetchall()
        print(data)
        cur.close()
        connection.close()
        i+=1
           

2.读

sql = “select * from petroleum where id in (‘%s’)” %(i)

2.写

二、使用步骤

1.引入库

代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context
           

2.读入数据

代码如下(示例):

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
           

该处使用的url网络请求的数据。