python

しょっちゅう忘れることを書いておく。

66

1160 views

テーブルを作成するとき

import sqlite3
from contextlib import closing

def create_table(db_path):
    with closing(sqlite3.connect(db_path)) as conn:
        c = conn.cursor()
        sql ="""CREATE TABLE if not exists [ticker] (
                [id] INTEGER,
                [last] INTEGER,
                [bid] INTEGER,
                [ask] INTEGER,
                [high] INTEGER,
                [low] INTEGER,
                [volume] INTEGER,
                [timestamp] INTEGER,
                PRIMARY KEY([id])
                );
            """
        c.execute(sql)
        conn.commit()

selectをするとき

import sqlite3
from contextlib import closing

sql = "select min(timestamp) from ticker"
def select(sql):
    results = []
    rows = c.execute(sql)
    for row in rows:
        results.append(row)

    return results

insertするとき

def insert_rate_pair(c, pair_name, rate, timestamp):
        sql = "insert into {}(rate, timestamp) values(?,?)".format(pair_name)
        holder = (rate, timestamp)
        c.execute(sql, holder)

Page 16 of 69.

前のページ 次のページ



[添付ファイル]


お問い合わせ

プロフィール

すぺぺぺ

自己紹介

本サイトの作成者。
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
ChatGPTで自動プログラム作成に取り組み中。

サイト/ブログ

https://www.osumoi-stdio.com/novel/

ツイッター

@darkimpact0626