Skip to content

Commit

Permalink
Update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Mar 28, 2020
1 parent 5af0b1e commit db84367
Show file tree
Hide file tree
Showing 27 changed files with 443 additions and 343 deletions.
27 changes: 16 additions & 11 deletions sample/create_table/sample_create_table_from_csv.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
from simplesqlite import SimpleSQLite


with open("sample_data.csv", "w") as f:
f.write("\n".join(['"attr_a","attr_b","attr_c"', '1,4,"a"', '2,2.1,"bb"', '3,120.9,"ccc"']))
def main():
with open("sample_data.csv", "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("sample_data.csv")
# create table ---
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_from_csv("sample_data.csv")

# output ---
table_name = "sample_data"
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
# output ---
table_name = "sample_data"
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)


if __name__ == '__main__':
main()
39 changes: 22 additions & 17 deletions sample/create_table/sample_create_table_from_data_matrix.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
from simplesqlite import SimpleSQLite


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

# 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_from_data_matrix(
table_name, ["attr_a", "attr_b", "attr_c", "attr_d", "attr_e"], data_matrix
)

# display data type for each column in the table -----
print(con.schema_extractor.fetch_table_schema(table_name).dumps())

# display values in the table -----
print("records:")
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
def main():
table_name = "sample_table"
con = SimpleSQLite("sample.sqlite", "w")

# 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_from_data_matrix(
table_name, ["attr_a", "attr_b", "attr_c", "attr_d", "attr_e"], data_matrix
)

# display data type for each column in the table -----
print(con.schema_extractor.fetch_table_schema(table_name).dumps())

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


if __name__ == '__main__':
main()
21 changes: 13 additions & 8 deletions sample/create_table/sample_create_table_from_dataframe.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
from simplesqlite import SimpleSQLite


con = SimpleSQLite("pandas_df.sqlite")

con.create_table_from_dataframe(
pandas.DataFrame(
[[0, 0.1, "a"], [1, 1.1, "bb"], [2, 2.2, "ccc"]], columns=["id", "value", "name"]
),
table_name="pandas_df",
)
def main():
con = SimpleSQLite("pandas_df.sqlite")

con.create_table_from_dataframe(
pandas.DataFrame(
[[0, 0.1, "a"], [1, 1.1, "bb"], [2, 2.2, "ccc"]], columns=["id", "value", "name"]
),
table_name="pandas_df",
)


if __name__ == '__main__':
main()
103 changes: 54 additions & 49 deletions sample/create_table/sample_create_table_from_excel.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,57 @@
import simplesqlite


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 = pytablereader.ExcelTableFileLoader(file_path)
for table_data in loader.load():
con.create_table_from_tabledata(table_data)

# output ---
for table_name in con.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()
def main():
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 = pytablereader.ExcelTableFileLoader(file_path)
for table_data in loader.load():
con.create_table_from_tabledata(table_data)

# output ---
for table_name in con.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()


if __name__ == '__main__':
main()
35 changes: 20 additions & 15 deletions sample/create_table/sample_create_table_from_gs.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@
import simplesqlite


credentials_file = "sample-xxxxxxxxxxxx.json"
def main():
credentials_file = "sample-xxxxxxxxxxxx.json"

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

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

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

# output ---
for table_name in con.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()
# output ---
for table_name in con.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()


if __name__ == '__main__':
main()
67 changes: 36 additions & 31 deletions sample/create_table/sample_create_table_from_json_multi.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,39 @@
from simplesqlite import SimpleSQLite


file_path = "sample_data_multi.json"

# create sample data file ---
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.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()
def main():
file_path = "sample_data_multi.json"

# create sample data file ---
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.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()


if __name__ == '__main__':
main()
53 changes: 29 additions & 24 deletions sample/create_table/sample_create_table_from_json_single.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
from simplesqlite import SimpleSQLite


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.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()
def main():
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.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()


if __name__ == '__main__':
main()

0 comments on commit db84367

Please sign in to comment.