Execute sqlite3 “dot” commands from Python or register collation in command line utility

前端 未结 6 1144
轮回少年
轮回少年 2021-02-07 01:34

My sqlite3 database contains a \"collate\" column-constraint. I\'ve placed it in the schema for the table, to prevent accidentally neglecting to use the necessary collation. How

相关标签:
6条回答
  • 2021-02-07 01:40

    The APSW Python wrapper for SQLite does include a shell compatible with the SQLite one. You can also extend it to add more commands of your own choosing, extra output modes etc. (Disclosure: I am the APSW author.)

    • APSW shell
    • Difference between APSW and pysqlite
    0 讨论(0)
  • 2021-02-07 01:45

    The .import command in the sqlite shell is a builtin command. It's processed by the shell program, not the SQL engine, so you can't execute it like an SQL statement.

    Reading code for SQLite's shell.c, it seems that .import is simply a loop, reading lines from the data file, splitting on the separator, and passing the fields as parameter values to a prepared INSERT statement. So you should be able to mimic the behavior of .import with Python code easily.

    I tested the following with Python 2.6:

    import sqlite3
    import csv
    
    conn = sqlite3.connect(':memory:')
    
    conn.execute('create table mytable (col1 text, col2 text, col3 text)')
    
    csvReader = csv.reader(open('mydata.csv'), delimiter=',', quotechar='"')
    
    for row in csvReader:
            conn.execute('insert into mytable (col1, col2, col3) values (?, ?, ?)', row)
    
    cur = conn.cursor()
    cur.execute('select * from mytable')
    print cur.fetchall()
    
    0 讨论(0)
  • 2021-02-07 01:50

    The dot commands are only available to the sqlite3 executable. You will have to replace them with the equivalent combination of Python code and DB-API calls in order to mimic their behavior.

    0 讨论(0)
  • 2021-02-07 01:57

    You can call dot commands from Python using the subprocess module, which basically invokes a shell. If you need to use multiple dot commands, you can pass them as separate shell arguments - using a semicolon to separate them won't work.

    import subprocess
    subprocess.call(["sqlite3", "xxx.db", 
      ".mode tabs", 
      ".import file.tsv table_name"])
    
    0 讨论(0)
  • 2021-02-07 02:02

    subprocess.run() is preferred oversubprocess.call(). Below is my answer:

    import subprocess
    schema = subprocess.run(
        ['sqlite3',
         'xxx.db',
         '.schema'
        ],
        capture_output=True
    ).output.decode('utf-8')
    
    print(schema)
    
    0 讨论(0)
  • 2021-02-07 02:03

    You can load new collating sequences and functions using load_extension() built-in SQLite function or .load command in command line shell for SQLite. Obviously, extensions shold be written in C.

    And you can not call dot-commands from python, because dot-commands are specific to the command line shell tool.

    0 讨论(0)
提交回复
热议问题