4.1. Create table

4.1.1. Create a table from data matrix

create_table_with_data() method can get create a table from data matrix. Data matrix is a list of dict/namedtuple()/list/tuple.

Sample code
from simplesqlite import SimpleSQLite
import six

con = SimpleSQLite("sample.sqlite", "w")
table_name = "sample_table"

# create table -----
data_matrix = [
    [1, 1.1, "aaa", 1,   1],
    [2, 2.2, "bbb", 2.2, 2.2],
    [3, 3.3, "ccc", 3,   "ccc"],
]
con.create_table_with_data(
    table_name="sample_table",
    attribute_name_list=["attr_a", "attr_b", "attr_c", "attr_d", "attr_e"],
    data_matrix=data_matrix)

# display values in the table -----
six.print_(con.get_attribute_name_list(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
    six.print_(record)

# display data type for each column in the table -----
six.print_(con.get_attribute_type_list(table_name))
Output
['attr_a', 'attr_b', 'attr_c', 'attr_d', 'attr_e']
(1, 1.1, u'aaa', 1.0, u'1')
(2, 2.2, u'bbb', 2.2, u'2.2')
(3, 3.3, u'ccc', 3.0, u'ccc')
(u'integer', u'real', u'text', u'real', u'text')

4.1.2. Create a table from a CSV file/text

create_table_from_csv() method can create a table from a CSV file/text.

Sample code
from simplesqlite import SimpleSQLite
import six

file_path = "sample_data.csv"

with open(file_path, "w") as f:
    f.write("\n".join([
        '"attr_a","attr_b","attr_c"',
        '1,4,"a"',
        '2,2.1,"bb"',
        '3,120.9,"ccc"',
    ]))

# create table ---
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_from_csv(file_path)

# output ---
table_name = "sample_data"
six.print_(con.get_attribute_name_list(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
    six.print_(record)
Output
['attr_a', 'attr_b', 'attr_c']
(1, 4.0, u'a')
(2, 2.1, u'bb')
(3, 120.9, u'ccc')

4.1.3. Create table(s) from JSON file/text

create_table_from_json() method can create a table from a JSON file/text.

Sample code 1: multiple table data in a file
from simplesqlite import SimpleSQLite
import six

file_path = "sample_data.json"

with open(file_path, "w") as f:
    f.write("""{
        "table_a" : [
            {"attr_b": 4, "attr_c": "a", "attr_a": 1},
            {"attr_b": 2.1, "attr_c": "bb", "attr_a": 2},
            {"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3}
        ],
        "table_b" : [
            {"a": 1, "b": 4},
            {"a": 2 },
            {"a": 3, "b": 120.9}
        ]
    }""")

# create table ---
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_from_json(file_path)

# output ---
for table_name in con.get_table_name_list():
    six.print_("table: " + table_name)
    six.print_(con.get_attribute_name_list(table_name))
    result = con.select(select="*", table_name=table_name)
    for record in result.fetchall():
        six.print_(record)
    six.print_()
Output of the sample code 1
table: table_b
['a', 'b']
(1, u'4')
(2, u'NULL')
(3, u'120.9')

table: table_a
['attr_a', 'attr_b', 'attr_c']
(1, 4.0, u'a')
(2, 2.1, u'bb')
(3, 120.9, u'ccc')
Sample code 2: single table data in a file
from simplesqlite import SimpleSQLite
import six

file_path = "sample_data_single.json"

# create sample data file ---
with open(file_path, "w") as f:
    f.write("""[
        {"attr_b": 4, "attr_c": "a", "attr_a": 1},
        {"attr_b": 2.1, "attr_c": "bb", "attr_a": 2},
        {"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3}
    ]""")

# create table ---
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_from_json(file_path)

# output ---
for table_name in con.get_table_name_list():
    six.print_("table: " + table_name)
    six.print_(con.get_attribute_name_list(table_name))
    result = con.select(select="*", table_name=table_name)
    for record in result.fetchall():
        six.print_(record)
    six.print_()
Output of the sample code 2
table: sample_data_single
['attr_a', 'attr_b', 'attr_c']
(1, 4.0, u'a')
(2, 2.1, u'bb')
(3, 120.9, u'ccc')

4.1.4. Create table(s) from a Excel file

ExcelTableFileLoader class and create_table_from_tabledata() method can create a table from a Excel file.

Sample code
import simplesqlite
import six
import xlsxwriter

file_path = "sample_data.xlsx"

# create sample data file ---
workbook = xlsxwriter.Workbook(file_path)

worksheet = workbook.add_worksheet("samplesheet1")
table = [
    ["", "", "", ""],
    ["", "a", "b", "c"],
    ["", 1, 1.1, "a"],
    ["", 2, 2.2, "bb"],
    ["", 3, 3.3, "cc"],
]
for row_idx, row in enumerate(table):
    for col_idx, item in enumerate(row):
        worksheet.write(row_idx, col_idx, item)

worksheet = workbook.add_worksheet("samplesheet2")

worksheet = workbook.add_worksheet("samplesheet3")
table = [
    ["", "", ""],
    ["", "", ""],
    ["aa", "ab", "ac"],
    [1, "hoge", "a"],
    [2, "", "bb"],
    [3, "foo", ""],
]
for row_idx, row in enumerate(table):
    for col_idx, item in enumerate(row):
        worksheet.write(row_idx, col_idx, item)

workbook.close()

# create table ---
con = simplesqlite.SimpleSQLite("sample.sqlite", "w")

loader = simplesqlite.loader.ExcelTableFileLoader(file_path)
for tabledata in loader.load():
    con.create_table_from_tabledata(tabledata)

# output ---
for table_name in con.get_table_name_list():
    six.print_("table: " + table_name)
    six.print_(con.get_attribute_name_list(table_name))
    result = con.select(select="*", table_name=table_name)
    for record in result.fetchall():
        six.print_(record)
    six.print_()
Output
table: samplesheet1
['a', 'b', 'c']
(1.0, 1.1, u'a')
(2.0, 2.2, u'bb')
(3.0, 3.3, u'cc')

table: samplesheet3
['aa', 'ab', 'ac']
(1.0, u'hoge', u'a')
(2.0, u'', u'bb')
(3.0, u'foo', u'')

4.1.5. Create table(s) from Google Sheets

GoogleSheetsTableLoader class and create_table_from_tabledata() method can create a table from Google Spreadsheet.

Sample code
import simplesqlite
import six

credentials_file = "sample-xxxxxxxxxxxx.json"

# create table ---
con = simplesqlite.SimpleSQLite("sample.sqlite", "w")

loader = simplesqlite.loader.GoogleSheetsTableLoader(credentials_file)
loader.title = "samplebook"

for tabledata in loader.load():
    con.create_table_from_tabledata(tabledata)

# output ---
for table_name in con.get_table_name_list():
    six.print_("table: " + table_name)
    six.print_(con.get_attribute_name_list(table_name))
    result = con.select(select="*", table_name=table_name)
    for record in result.fetchall():
        six.print_(record)
    six.print_()