如何使用Python在sqlite3中保留“'(单引号)”

时间:2019-10-29 05:01:58

标签: python python-3.x sqlite

我的环境:

  • Python 3.6.5
  • SQLite 3.28.0

我正在努力在sqlite3数据库中保存单引号。

import sqlite3


class Database:
    def __init__(self):
        self.dbpath = 'db.sqlite3'
        self.conn = sqlite3.connect(self.dbpath)
        self.c = self.conn.cursor()


db = Database()


def update_comment(name, comment='null'):
    db.c.execute('update twitter_users set comment = ? where name = ?', (comment, name))
    db.conn.commit()


update_comment('Jikkenndesu', "How's day?")

但是,如果我执行select * from twitter_users;,它将输出Jikkenndesu|Hows day?。单引号被取消。

如何解决此问题? `

1 个答案:

答案 0 :(得分:1)

我通过代码打击解决了这个问题:

import sqlite3


class Database:
    def __init__(self):
        self.dbpath = 'db.sqlite3'
        self.conn = sqlite3.connect(self.dbpath)
        self.c = self.conn.cursor()


db = Database()


def update_comment(name, comment='null'):
    db.c.execute("""update twitter_users set comment = ? where name = ?""", (comment, name))
    db.conn.commit()


update_comment('Jikkenndesu', "It's a bad day today, and I will go to Phillip's")