2021年3月26日 星期五

【python】sqlite3資料庫:新增、刪除、修改、查詢、顯示的範例

最近用到,範例程式碼如後:

在Python3.x 中raw_input() 和input() 進行了整合,去除了raw_input( ),僅保留了input( )函數,其接收任意任性輸入,將所有輸入默認為字符串處理,並返回字符串類型。

  1. # coding=utf-8
  2. __auther__ = 'xianbao'
  3. import sqlite3
  4. # 開啟資料庫
  5.  
  6.  
  7. def opendata():
  8. conn = sqlite3.connect("test.db")
  9. cur = conn.execute("""create table if not exists userTB (ID integer primary key, IP CHAR(50))""")
  10. return cur, conn
  11. # 查詢全部的資訊
  12.  
  13.  
  14.  
  15. def showalldata():
  16. print("-------------------處理後後的資料-------------------")
  17. hel = opendata()
  18. cur = hel[1].cursor()
  19. cur.execute("select * from userTB")
  20. res = cur.fetchall()
  21. for line in res:
  22. for h in line:
  23. print(h)
  24. print()
  25. cur.close()
  26. # 輸入資訊
  27.  
  28.  
  29. def into():
  30. userID = str(input("請輸入使用者ID:"))
  31. userIP = str(input("請輸入使用者IP:"))
  32. return userID, userIP
  33. # (新增) 往資料庫中新增內容
  34.  
  35.  
  36. def adddata():
  37. welcome = """-------------------歡迎使用新增資料功能---------------------"""
  38. print(welcome)
  39. person = into()
  40. hel = opendata()
  41. hel[1].execute("insert into userTB(ID, IP)values (?,?)", (person[0], person[1]))
  42. hel[1].commit()
  43. print("-----------------恭喜你資料,新增成功----------------")
  44. showalldata()
  45. hel[1].close()
  46. # (刪除)刪除資料庫中的內容
  47.  
  48.  
  49. def deldata():
  50. welcome = "------------------歡迎您使用刪除資料庫功能------------------"
  51. print(welcome)
  52. delchoice = input("請輸入您想要刪除使用者ID:")
  53. hel = opendata() # 返回遊標conn
  54. hel[1].execute("delete from userTB where ID ="+delchoice)
  55. hel[1].commit()
  56. print("-----------------恭喜你資料,刪除成功----------------")
  57. showalldata()
  58. hel[1].close()
  59. # (修改)修改資料的內容
  60.  
  61.  
  62. def alter():
  63. welcome = "--------------------歡迎你使用修改資料庫功能-----------------"
  64. print(welcome)
  65. changechoice = input("請輸入你想要修改的使用者ID:")
  66. hel = opendata()
  67. person = into()
  68. hel[1].execute("update userTB set ID=?, IP= ? where id="+changechoice, (person[0], person[1]))
  69. hel[1].commit()
  70. showalldata()
  71. hel[1].close()
  72. # 查詢資料
  73.  
  74.  
  75. def searchdata():
  76. welcome = "--------------------歡迎你使用查詢資料庫功能-----------------"
  77. print(welcome)
  78. choice = str(input("請輸入你要查詢的使用者ID:"))
  79. hel = opendata()
  80. cur = hel[1].cursor()
  81. cur.execute("select * from userTB where ID="+choice)
  82. hel[1].commit()
  83. row = cur.fetchone()
  84. id1 = str(row[0])
  85. ip1 = str(row[1])
  86. print("-------------------恭喜你,你要查詢的資料如下---------------------")
  87. print("您查詢的資料ID是%s" % id1)
  88. print("您查詢的資料IP是%s" % ip1)
  89. cur.close()
  90. hel[1].close()
  91. # 是否繼續
  92.  
  93.  
  94. def contnue1(a):
  95. choice = input("是否繼續?(y or n):")
  96. if choice == 'y':
  97. a = 1
  98. else:
  99. a = 0
  100. return a
  101.  
  102.  
  103. if __name__ == "__main__":
  104. flag = 1
  105. while flag:
  106. welcome = "---------歡迎使用資料庫通訊錄---------"
  107. print(welcome)
  108. choiceshow = """
  109. 請選擇您的進一步選擇:
  110. (a)往資料庫裡面新增內容
  111. (d)刪除資料庫中內容
  112. (m)修改書庫的內容
  113. (q)查詢資料的內容
  114. (s)顯示所有資料內容
  115. 選擇您想要的進行的操作:
  116. """
  117. choice = input(choiceshow)
  118. if choice == "a":
  119. adddata()
  120. contnue1(flag)
  121. elif choice == "d":
  122. deldata()
  123. contnue1(flag)
  124. elif choice == "m":
  125. alter()
  126. contnue1(flag)
  127. elif choice == "q":
  128. searchdata()
  129. contnue1(flag)
  130. elif choice == "s":
  131. showalldata()
  132. contnue1(flag)
  133. else:
  134. print("你輸入錯誤,請重新輸入")
  135.  



沒有留言:

張貼留言