python

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

33

914 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 56.

前のページ 次のページ



[添付ファイル]


お問い合わせ

プロフィール

マッスル

自己紹介

本サイトの作成者。
趣味:プログラム/水耕栽培/仮想通貨/激辛好き
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
仮想通貨はNEMが好き。
水耕栽培は激辛好きが高じて、キャロライナ・リーパーの栽培にチャレンジ中。

サイト/ブログ

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

ツイッター

@darkimpact0626