0

My environment:

  • Python 3.6.5
  • SQLite 3.28.0

I am struggling to save a single quote in the sqlite3 database.

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?")

But if I execute select * from twitter_users;, it output Jikkenndesu|Hows day?. The single quote is banished.

How can I solve this issue? `

kai00
  • 57
  • 1
  • 11
  • Not getting this issue in my setup. Can you provide the schema for `twitter_users`? – kuro Oct 29 '19 at 05:22
  • Are those of any help here? 1) https://stackoverflow.com/questions/15200461/insert-single-quotes-in-sqlite?lq=1 2) https://stackoverflow.com/questions/603572/escape-single-quote-character-for-use-in-an-sqlite-query – Denis Rasulev Oct 29 '19 at 05:29
  • Thank you, guys. But it seems not to be a help for me. – kai00 Oct 29 '19 at 05:49
  • The sqlite3 file is here.https://www.dropbox.com/s/xtvmrjmg5tkgtb7/db.sqlite3?dl=0 – kai00 Oct 29 '19 at 05:51
  • it storing fine in db, it is str in terminal so you dont need to worry about htis – sahasrara62 Oct 29 '19 at 06:09

1 Answers1

1

I solved this issue with code blow:

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")
kai00
  • 57
  • 1
  • 11