make_db.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import sqlite3
  2. conn = sqlite3.connect('vps2021.db')
  3. c = conn.cursor()
  4. # Create table 建立 vps 数据表
  5. c.execute('''CREATE TABLE vps
  6. (ip text, port text, password text, ss_port text, info text)''')
  7. # Insert a row of data 插入一行示例数据
  8. c.execute('''INSERT INTO vps VALUES
  9. ('188.188.188.188', '22', 'passwd@vps2021', '443', '0号vps示例')''')
  10. # Save (commit) the changes 保存提交数据修改
  11. conn.commit()
  12. # 不应该使用 Python 的字符串操作来创建你的查询语句,因为那样做不安全;它会使你的程序容易受到 SQL 注入攻击
  13. # 推荐使用 DB-API 的参数替换。在 SQL 语句中,使用 ? 占位符来代替值,然后把对应的值组成的元组做为 execute() 方法的第二个参数。
  14. port = ('22',)
  15. c.execute('SELECT * FROM vps WHERE port=?', port)
  16. print(c.fetchone())
  17. # Larger example that inserts many records at a time
  18. # 一次插入很多行数据记录的例子
  19. purchases = [ ('188.188.188.188', '10122', 'passwd@vps2021', '443', '1号NAT小鸡'),
  20. ('188.188.188.188', '10222', 'passwd@vps2021', '443', '2号NAT小鸡'),
  21. ('188.188.188.188', '10322', 'passwd@vps2021', '443', '3号NAT小鸡'),
  22. ('188.188.188.188', '10422', 'passwd@vps2021', '443', '4号NAT小鸡'),
  23. ('188.188.188.188', '10522', 'passwd@vps2021', '443', '5号NAT小鸡'),
  24. ('188.188.188.188', '10622', 'passwd@vps2021', '443', '6号NAT小鸡'),
  25. ('188.188.188.188', '10722', 'passwd@vps2021', '443', '7号NAT小鸡'),
  26. ('188.188.188.188', '10822', 'passwd@vps2021', '443', '8号NAT小鸡'),
  27. ('188.188.188.188', '10922', 'passwd@vps2021', '443', '9号NAT小鸡'),
  28. ]
  29. c.executemany('INSERT INTO vps VALUES (?,?,?,?,?)', purchases)
  30. conn.commit()
  31. # We can also close the connection if we are done with it.
  32. # 如果我们完成了连接,我们也可以关闭连接。
  33. # Just be sure any changes have been committed or they will be lost.
  34. # 只要确定任何修改都已经提交,否则就会丢失。
  35. conn.close()