8.7python

#!/usr/bin/python3
#
导入数据库
import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "root", "123456", "test")

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()

print("Database version : %s " % data)

# 关闭数据库连接
db.close()


#使用预处理语句创建表
sql = """create table emp(
        FIRST_NAME CHAR(20) NOT NULL,
        LAST_NAME CHAR (20),
        AGE INT ,
        SEX CHAR (1),
        INCOME FLOAT ) """




#导入数据
import pymysql
#打开数据库连接
db = pymysql.connect("localhost","root","123456","test")
#使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
#使用execute()方法执行SQL,如果存在则和删除
#cursor.execute("drop table if EXISTS emp")
#创建数据表
#cursor.execute("create table emp(FIRST_NAME CHAR (20) NOT NULL ,LAST_NAME varchar(20),AGE INT ,SEX CHAR (1),INCOME FLOAT)")
#插入数据
sql = """INSERT INTO emp (FIRST_NAME,LAST_NAME,AGE,SEX,INCOME)
          VALUES ('Mac','Mohan',20,'M',2000)
        """
try:
    #执行sql语句
    cursor.execute(sql)
    # 执行sql语句
    db.commit()
except:
    #发生错误时回滚
    db.rollback()
#关闭数据库
db.close()




#查询emp表工资大于1000的员工信息
#导入数据
import pymysql
#打开数据库连接
db = pymysql.connect("localhost","root","123456","test")
#使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
#SQL查询语句
sql = "SELECT * FROM emp WHRER INCOME > 1000 "
try:
    cursor.execute(sql)
    results = cursor.fetchall()
    for row in results:
        fname = row[0]
        lname = row[1]
        age = row[2]
        sex = row[3]
        income = row[4]

        print("fname = %s,lname = %s,age = %d,sex = %s,income = %d" ,fname,lname,age,sex,income)
except:
    print("error")

db.close()





import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "root", "123456", "test")

# 使用cursor()方法获取操作游标
cursor = db.cursor()

# SQL 查询语句
sql = "SELECT * FROM emp \
       WHERE INCOME > '%d'" % (1000)
try:
    # 执行SQL语句
    cursor.execute(sql)
    # 获取所有记录列表
    results = cursor.fetchall()
    for row in results:
        fname = row[0]
        lname = row[1]
        age = row[2]
        sex = row[3]
        income = row[4]
        # 打印结果
        print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
              (fname, lname, age, sex, income))
except:
    print("Error: unable to fetch data")

# 关闭数据库连接
db.close()






#类、面向对象

class MyClass():
    i = 123456
    def f(self):
        return 'hello world'

#实例化类
x = MyClass()

#访问类的属性和方法
print("MyClass 类的属性i为:",x.i)
print("MyClass 类的方法f输出为:",x.f())




class Comp:
    def __init__(self,realpart,imagpart):
        self.r = realpart
        self.i = imagpart
x = Comp(3.0,-4.5)
print(x.r,x.i)




class Test:
    def prt(self):
        print(self)
        print(self.__class__)
t = Test()
t.prt()



#定义一个人的类,定义名字年龄为 基本属性,体重为私有属性
class people:
    name = ''
    age = 0
    __weight = 0
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w
    def speak(self):
        print("%s 说:我 %d 岁" %(self.name,self.age))
#print("%s 说:我 %d 岁" %(self.name,self.age))中间不需要加标点符号
#实例化类
p = people('lili',20,56)
p.speak()

 

猜你喜欢

转载自blog.csdn.net/lfr1095767459/article/details/82152027