当前位置: 首页 > news >正文

域名价格查询网站营销软文范例500

域名价格查询网站,营销软文范例500,网站代发怎么做,深圳网站制作公司方案文章目录 连接Mysql数据库安装Mysql数据库连接数据库创建数据库创建数据表查询表是否存在设置主键插入数据批量插入查询、删除、更新数据 使用PyMySql连接数据库安装PyMySql连接数据库 连接MongoDB安装pymongo驱动在MongoDB创建库及数据插入文档查询数据修改数据文档排序删除数…

文章目录

  • 连接Mysql数据库
    • 安装Mysql数据库
    • 连接数据库
      • 创建数据库
      • 创建数据表
      • 查询表是否存在
      • 设置主键
      • 插入数据
      • 批量插入
      • 查询、删除、更新数据
  • 使用PyMySql连接数据库
    • 安装PyMySql
    • 连接数据库
  • 连接MongoDB
    • 安装pymongo驱动
    • 在MongoDB创建库及数据
    • 插入文档
    • 查询数据
    • 修改数据
    • 文档排序
    • 删除数据

连接Mysql数据库

使用mysql-connector连接mysql数据库

注意:如果你的 MySQL 是 8.0 版本,密码插件验证方式发生了变化,早期版本为 mysql_native_password,8.0 版本为 caching_sha2_password,所以需要做些改变:

  • 先修改 my.ini 配置:
[mysqld]
default_authentication_plugin=mysql_native_password
  • 然后在 mysql 下执行以下命令来修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

安装Mysql数据库

python -m pip install mysql-connector

连接数据库

创建数据库

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="root"
)mycursor = mydb.cursor()
#创建数据库,如果存在会报错
# mycursor.execute("CREATE DATABASE runoob_db")
# 查询所有的数据库
mycursor.execute("SHOW DATABASES")for x in mycursor:print(x)

创建数据表

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="root",database="runoob_db" # 可选
)mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE sites (name VARCHAR(255),url VARCHAR(255))")

查询表是否存在

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="root",database="runoob_db" 
)mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:print(x)

设置主键

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="root",database="runoob_db" 
)mycursor = mydb.cursor()
mycursor.execute("ALTER TABLE sites ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")# 表未创建
mycursor.execute("CREATE TABLE sites (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), url VARCHAR(255))")

插入数据

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="root",database="runoob_db" # 可选
)mycursor = mydb.cursor()
sql = "INSERT INTO sites (name,url) VALUES (%s,%s)"
val = ("RUNOOB","https://www.runoob.com")
mycursor.execute(sql,val)mydb.commit() # 数据表内容有更新,必须使用该语句
print(mycursor.rowcount,"记录插入成功。")

批量插入

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="root",database="runoob_db" # 可选
)mycursor = mydb.cursor()
sql = "INSERT INTO sites (name,url) VALUES (%s,%s)"
# val = ("RUNOOB","https://www.runoob.com")
# 批量插入
val = [('Baidu','https://www.baidu.com'),('Taobao','https://www.taobao.com'),('Jingdong','https://www.jd.com'),('tencent','https://www.tencent.com'),('huaxing','https://www.sdhxem.com')
]
# 批量插入使用executemany()方法
mycursor.executemany(sql,val)mydb.commit() # 数据表内容有更新,必须使用该语句
print(mycursor.rowcount,"记录插入成功。")

查询、删除、更新数据

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="root",database="runoob_db" # 可选
)mycursor = mydb.cursor()# 指定字段查询
# mycursor.execute("SELECT name,url FROM sites")
# 查询全部
#mycursor.execute("SELECT * FROM sites")
# myresult = mycursor.fetchall()
# for x in myresult:
#     print(x)
# 查询一条
# myresult1 = mycursor.fetchone()
# print(myresult1)
# where 条件语句
# sql = "SELECT * FROM sites WHERE name = 'RUNOOB'"
# mycursor.execute(sql)
# myresult = mycursor.fetchall()
# for x in myresult:
#     print(x)
# 使用通配符
# sql = "SELECT * FROM sites where url like '%oo%'"
# mycursor.execute(sql)
# myresult = mycursor.fetchall()
# for x in myresult:
#     print(x)
# 使用占位符
# sql = "SELECT * FROM sites WHERE name =%s"
# na = ("RUNOOB",)
# mycursor.execute(sql,na)
# myresult = mycursor.fetchall()
# for x in myresult:
#     print(x)# 排序
# sql = "SELECT * FROM sites ORDER BY name"
# mycursor.execute(sql)
# myresult = mycursor.fetchall()
# for x in myresult:
#     print(x)# limit
# sql = "SELECT * FROM sites LIMIT 2,3"
# mycursor.execute(sql)
# myresult = mycursor.fetchall()
# for x in myresult:
#     print(x)# OFFSET
# sql = "SELECT * FROM sites LIMIT 3 OFFSET 1"
# mycursor.execute(sql)
# myresult = mycursor.fetchall()
# for x in myresult:
#     print(x)# 删除数据
# sql = "DELETE FROM sites WHERE name = 'RUNOOB'"
# mycursor.execute(sql)
#
# mydb.commit()
#
# print(mycursor.rowcount,"条记录被删除")# 更新数据
sql = "UPDATE sites SET name = 'TAOBAO' WHERE name = 'Taobao'"
mycursor.execute(sql)mydb.commit()print(mycursor.rowcount,"条记录被修改")

使用PyMySql连接数据库

安装PyMySql

pip3 install PyMySQL

连接数据库

import pymysql
# 1.编辑数据库连接
db = pymysql.connect(host='localhost',user='root',password='root',database='runoob_db'
)
# 2.创建游标对象
cursor = db.cursor()
# 3.执行sql语句
# cursor.execute("SELECT VERSION()")
# 4.获取数据库数据
# data = cursor.fetchone()
# 5.处理结果
# print("Database version : %s"% data)# 创建表
# cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
# sql = """CREATE TABLE EMPLOYEE (FIRST_NAME CHAR(20) NOT NULL,
#                                 LAST_NAME CHAR(20),
#                                 AGE INT,
#                                 SEX CHAR(1),
#                                 INCOME FLOAT)"""
# cursor.execute(sql)# 插入数据
# sql = """INSERT INTO EMPLOYEE(FIRST_NAME,LAST_NAME,AGE,SEX,INCOME)
#                         VALUES('Mac','Mohan',20,'M',2000)"""
# try:
#     cursor.execute(sql)
#     db.commit()
# except:
#     db.rollback()# 数据库查询
# sql = "SELECT * FROM EMPLOYEE \
#        WHERE INCOME > %s" % (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=%s,sex=%s,income=%s" % (fname,lname,age,sex,income))
# except:
#     print("Error: unable to fetch data")# 数据库更新操作
# sql = "UPDATE EMPLOYEE SET AGE=AGE+1 WHERE SEX = '%c'" % ('M')
# try:
#     cursor.execute(sql)
#     db.commit()
# except:
#     db.rollback()# 删除操作
sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (20)
try:cursor.execute(sql)db.commit()
except:db.rollback()
# 6.关闭数据库连接
db.close()

连接MongoDB

安装pymongo驱动

pip3 install pymongo

在MongoDB创建库及数据

注意: 在 MongoDB 中,集合只有在内容插入后才会创建! 就是说,创建集合(数据表)后要再插入一个文档(记录),集合才会真正创建。

import pymongo# 创建数据库
myclient = pymongo.MongoClient("mongodb://localhost:27017/")# 创建数据库
mydb = myclient["runoobdb"]#判断数据库是否存在
# dblist = myclient.list_database_names()
# if "runoobdb" in dblist:
#     print("数据库已存在")# 创建集合
mycol = mydb["sites"]# 插入数据
mydict = {"name":"RUNOOB","alexa":"1000","url":"https://www.runoob.com"}
x = mycol.insert_one(mydict)#打印返回结果
print(x)

插入文档

import pymongo# # 创建数据库
# myclient = pymongo.MongoClient("mongodb://localhost:27017/")
# # 创建数据库:有则引用,无则创建
# mydb = myclient["runoobdb"]
# #判断数据库是否存在
# dblist = myclient.list_database_names()
# if "runoobdb" in dblist:
#     print("数据库已存在")
# mycol = mydb["sites"]
# # 返回_id字段
# mydict = {"name":"Google","alexa":"1","url":"https://www.google.com"}
# x = mycol.insert_one(mydict)
# #打印_id
# print(x.inserted_id)# 插入多个文档
# myclient = pymongo.MongoClient("mongodb://localhost:27017/")
# mydb = myclient["runoobdb"]
# mycol = mydb["sites"]
# mylist = [
#     {"name":"Taobao","alexa":"100","url":"https://www.taobao.com"},
#     {"name":"QQ","alexa":"101","url":"https://www.qq.com"}
# ]
#
# x = mycol.insert_many(mylist)
#
# print(x.inserted_ids)# 插入指定_id的多个文档
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
mycol = mydb["sites"]mylist = [{"_id":1,"name":"Facebook","address":"脸书"},{"_id":2,"name":"Taobao","address":"淘宝"}
]x = mycol.insert_many(mylist)print(x.inserted_ids)

查询数据

import pymongo
from pymongo.response import PinnedResponsemyclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
mycol = mydb["sites"]# 查询单条数据
# x = mycol.find_one()
# print(x)# 查询集合中所有数据
# for x in mycol.find():
#     print(x)# 查询指定字段数据,返回字段指定为1,非返回字段指定为0
# 除了 _id,你不能在一个对象中同时指定 0 和 1,如果你设置了一个字段为 0,则其他都为 1,反之亦然。
# for x in mycol.find({},{"_id":0,"name":1,"alexa":1}):
#     print(x)
# for x in mycol.find({},{"alexa":0}):
#     print(x)# 指定条件查询
# myquery = {"name":"RUNOOB"}
# mydoc = mycol.find(myquery)
#
# for x in mydoc:
#     print(x)# 高级查询 第一个字母 ASCII 值大于 "H" 的数据
# myquery = {"name":{"$gt":"H"}}
# mydoc = mycol.find(myquery)
# for x in mydoc:
#     print(x)# 使用正则表达式
# myquery = {"name":{"$regex":"^R"}}
# mydoc = mycol.find(myquery)
# for x in mydoc:
#     print(x)# 返回指定条数记录
myresult = mycol.find().limit(3)for x in myresult:print(x)

修改数据

import  pymongomyclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
mycol = mydb["sites"]# myquery = {"alexa":"1000"}
# newvalues = {"$set":{"alexa":"12345"}}
#
# mycol.update_one(myquery,newvalues)
#
# for x in mycol.find():
#     print(x)myquery = {"name":{"$regex":"^F"}}
newvalues = {"$set":{"alexa":"123"}}
x = mycol.update_many(myquery,newvalues)
print(x.modified_count,"条文档已修改")

文档排序

import pymongomyclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
mycol = mydb["sites"]
# 对字段alexa升序排序
# mydoc = mycol.find().sort("alexa")
#
# for x in mydoc:
#     print(x)# 对字段alexa降序排序
mydoc = mycol.find().sort("alexa",-1)
for x in mydoc:print(x)

删除数据

import pymongomyclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
mycol = mydb["sites"]# myquery = {"name":"RUNOOB"}
# # 删除数据
# mycol.delete_one(myquery)
# # 查询数据
# for x in mycol.find():
#     print(x)# 删除多个文档
# myquery = {"name":{"$regex":"^F"}}
# x = mycol.delete_many(myquery)
# print(x.deleted_count,"个文档已删除")# 删除集合中的所有文档
# x = mycol.delete_many({})
# print(x.deleted_count,"个文档已删除")#删除集合
mycol.drop()

文章转载自:
http://dinncogender.tqpr.cn
http://dinncononfluency.tqpr.cn
http://dinncowarehouseman.tqpr.cn
http://dinncowheal.tqpr.cn
http://dinncoearl.tqpr.cn
http://dinncosatanically.tqpr.cn
http://dinncoosteometrical.tqpr.cn
http://dinncopalmetto.tqpr.cn
http://dinncocrook.tqpr.cn
http://dinncoeurogroup.tqpr.cn
http://dinncooom.tqpr.cn
http://dinncotelemotor.tqpr.cn
http://dinncostrangles.tqpr.cn
http://dinncoably.tqpr.cn
http://dinncomoppy.tqpr.cn
http://dinncoacidy.tqpr.cn
http://dinncobrewage.tqpr.cn
http://dinncodrily.tqpr.cn
http://dinncowadeable.tqpr.cn
http://dinncojadeite.tqpr.cn
http://dinncoguardianship.tqpr.cn
http://dinncosightly.tqpr.cn
http://dinncothd.tqpr.cn
http://dinncophlogistic.tqpr.cn
http://dinncowoolgathering.tqpr.cn
http://dinncowalter.tqpr.cn
http://dinncoportrait.tqpr.cn
http://dinncocelestine.tqpr.cn
http://dinncohuppah.tqpr.cn
http://dinncofruticose.tqpr.cn
http://dinncomicroencapsulate.tqpr.cn
http://dinncoadiaphoretic.tqpr.cn
http://dinncomegaron.tqpr.cn
http://dinncotertio.tqpr.cn
http://dinncoflanker.tqpr.cn
http://dinncosbc.tqpr.cn
http://dinncomaddening.tqpr.cn
http://dinncorousing.tqpr.cn
http://dinncojacquette.tqpr.cn
http://dinncoabove.tqpr.cn
http://dinncosupermarket.tqpr.cn
http://dinncohomothermal.tqpr.cn
http://dinncoeudiometric.tqpr.cn
http://dinncoresultative.tqpr.cn
http://dinncocaleche.tqpr.cn
http://dinncocalciphobe.tqpr.cn
http://dinncophonometer.tqpr.cn
http://dinncoinsure.tqpr.cn
http://dinncosoffit.tqpr.cn
http://dinncomeditate.tqpr.cn
http://dinncoslouch.tqpr.cn
http://dinncooutgrowth.tqpr.cn
http://dinncodebug.tqpr.cn
http://dinncobilirubin.tqpr.cn
http://dinncosportsdom.tqpr.cn
http://dinncooerlikon.tqpr.cn
http://dinncopullulate.tqpr.cn
http://dinnconegotiant.tqpr.cn
http://dinncoclaustrophilia.tqpr.cn
http://dinncoprevalency.tqpr.cn
http://dinnconewswire.tqpr.cn
http://dinncodepersonalization.tqpr.cn
http://dinncovoicespond.tqpr.cn
http://dinncomajor.tqpr.cn
http://dinncosquamulate.tqpr.cn
http://dinncovideoize.tqpr.cn
http://dinncodignified.tqpr.cn
http://dinncofindable.tqpr.cn
http://dinncohotliner.tqpr.cn
http://dinncofungistat.tqpr.cn
http://dinncoenterpriser.tqpr.cn
http://dinncopleonastic.tqpr.cn
http://dinncomolasses.tqpr.cn
http://dinncomercaptide.tqpr.cn
http://dinncoichthyofauna.tqpr.cn
http://dinncopapable.tqpr.cn
http://dinncobrittle.tqpr.cn
http://dinncoureter.tqpr.cn
http://dinncoazotise.tqpr.cn
http://dinncounbathed.tqpr.cn
http://dinncodiomede.tqpr.cn
http://dinncomicrogametocyte.tqpr.cn
http://dinncoheadship.tqpr.cn
http://dinncohairstylist.tqpr.cn
http://dinncotautochronous.tqpr.cn
http://dinncobimeby.tqpr.cn
http://dinncobelong.tqpr.cn
http://dinncotaibei.tqpr.cn
http://dinncopriapean.tqpr.cn
http://dinnconailhole.tqpr.cn
http://dinncowakefield.tqpr.cn
http://dinncobarred.tqpr.cn
http://dinncofavose.tqpr.cn
http://dinncomelancholy.tqpr.cn
http://dinncoleonore.tqpr.cn
http://dinncokhowar.tqpr.cn
http://dinnconested.tqpr.cn
http://dinncoelectroplate.tqpr.cn
http://dinncoshadrach.tqpr.cn
http://dinncogneissose.tqpr.cn
http://www.dinnco.com/news/161400.html

相关文章:

  • 青海省建设厅官方网站建设云seo推广排名重要吗
  • 电商网站建设精准扶贫的目的营销策划推广
  • 网网站建设宁波seo怎么做引流推广
  • 货源网站 源码新网店怎么免费推广
  • 做网站一般需要什么seo店铺描述
  • 医院网站建设平台什么是seo优化?
  • 自己做网站需要多少钱上海网站设计公司
  • 卡通风格网站欣赏网上竞价
  • 北京做公司网站seo自己怎么做
  • 中山网站建设文化策划书产品推广方式都有哪些
  • 做货代的可以在哪些网站打广告2022新闻热点事件简短30条
  • 贷款超市网站开发百度上怎么免费开店
  • 网站建设加盟培训搜索引擎推广的三种方式
  • 广西安策企业管理咨询有限公司对网站提出的优化建议
  • 网页设计实训报告总结与体会seo从入门到精通
  • 百度联盟怎么做网站加入微博推广方法有哪些
  • 汽修专业主要学什么外贸seo公司
  • 昆明做百度网站电话正规seo关键词排名哪家专业
  • 如何自己做软件网站seo营销优化软件
  • 网站建设未完成网站优化是什么意思
  • 响应式布局网站开发黑帽seo365t技术
  • 十年前网站开发语言网站设计公司有哪些
  • php旅游网站论文淘宝关键词热度查询工具
  • 网站所有者是什么意思百度客服人工在线咨询电话
  • 成都门户网站建设多少钱app推广接单平台哪个好
  • 东莞品牌网站建设服务网络推广平台有哪些
  • 淘客网站自己做网页设计个人主页
  • wordpress中国服务器郑州网站制作选择乐云seo
  • 某网站开发项目成本估计chatgpt入口
  • 企业网站备案后可否更改名称seo商学院