【メモ】爬虫類+データベース操作(添削・見直し)
2368 ワード
基本操作:
import pymysql #
conn =pymysql.connect(
host ='127.0.0.1',
port =3307,
user ='root',
passwd='xhdake',
db ='CSRF',
charset ='utf8',
)
#
cursor =conn.cursor()
#( )
rab =cursor.execute('SELECT * FROM "sqlite_sequence"') #
data =cursor.fetchone() #
data =cursor.fetchall() #
print(rab)
conn.close() #
------------------------------------------------------------------------------
新規データベース:
方法一:(1)単一データの追加def xinz():
sql ="""INSERT INTO sqlite_sequence(name,seq)VALUES('abc',245)"""#
cursor.execute(sql)
conn.commit()
conn.close() #
方法2:(1)複数のデータを追加し、引数(数値定数)def xinz2():
name ='jing1'
seq ='11'
sql ="""INSERT INTO sqlite_sequence(name,seq)VALUES(%s,%s)""" #%s---
cursor.execute(sql,(name,seq)) #%s
conn.commit()
conn.close() #
データベースの表示:
一:正確な検索
def chaz():
# SELECT 1, 2 FROM WHERE 1 =abc
sql="""SELECT name,seq FROM sqlite_sequence WHERE name='abc';"""
cursor.execute(sql)
result =cursor.fetchone()#
print(result)
conn.close() #
二:ファジイクエリ:(すべてのデータを表示)#
def chaz2():
sql="""SELECT * FROM sqlite_sequence"""
cursor.execute(sql)
while True:
result=cursor.fetchone()
if result:
print()
else:
break
conn.close() #
3:Null値クエリー(fetchall()値はすべてのデータを表示) cursor.fetchmany(3) # 3
def chaz3():
# SELECT 1, 2 FROM WHERE 1 =abc
sql="""SELECT name,seq FROM sqlite_sequence"""
cursor.execute(sql)
results =cursor.fetchall()#
for result in results:
print(result)
conn.close() #
四:データを削除する
#データベースの列を削除
def shanc4(): sql ="""delete from user where id=1""" cursor.execute(sql)conn.commit()conn.close()データベースを閉じる5:データを更新def gengx 5():sql=""update user name='aaa'id='1'""cursor.execute(sql)conn.commit()conn.close()データベースを閉じる
import pymysql #
conn =pymysql.connect(
host ='127.0.0.1',
port =3307,
user ='root',
passwd='xhdake',
db ='CSRF',
charset ='utf8',
)
#
cursor =conn.cursor()
#( )
rab =cursor.execute('SELECT * FROM "sqlite_sequence"') #
data =cursor.fetchone() #
data =cursor.fetchall() #
print(rab)
conn.close() #
def xinz():
sql ="""INSERT INTO sqlite_sequence(name,seq)VALUES('abc',245)"""#
cursor.execute(sql)
conn.commit()
conn.close() #
def xinz2():
name ='jing1'
seq ='11'
sql ="""INSERT INTO sqlite_sequence(name,seq)VALUES(%s,%s)""" #%s---
cursor.execute(sql,(name,seq)) #%s
conn.commit()
conn.close() #
def chaz():
# SELECT 1, 2 FROM WHERE 1 =abc
sql="""SELECT name,seq FROM sqlite_sequence WHERE name='abc';"""
cursor.execute(sql)
result =cursor.fetchone()#
print(result)
conn.close() #
#
def chaz2():
sql="""SELECT * FROM sqlite_sequence"""
cursor.execute(sql)
while True:
result=cursor.fetchone()
if result:
print()
else:
break
conn.close() #
cursor.fetchmany(3) # 3
def chaz3():
# SELECT 1, 2 FROM WHERE 1 =abc
sql="""SELECT name,seq FROM sqlite_sequence"""
cursor.execute(sql)
results =cursor.fetchall()#
for result in results:
print(result)
conn.close() #