Skip to content

Commit

Permalink
Merge pull request #7 from thombashi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
thombashi committed Mar 11, 2016
2 parents 20f1dfa + 4bfbc98 commit a8a4cc3
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 97 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ README.md
misc/README_HEADER.rst
misc/readme_converter.py
upgrade.sh
sandbox/
125 changes: 87 additions & 38 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Feature
- Automatic table creation from data
- Support various data type for insertion : dictionary, namedtuple,
list and tuple
- Create table from csv file

Installation
============
Expand All @@ -37,54 +38,99 @@ Usage
Create table
------------

Create table from data matrix
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python
from simplesqlite import SimpleSQLite
con = SimpleSQLite("sample.sqlite")
# 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,
index_attribute_list=["attr_a"]
)
data_matrix=data_matrix)
# display values -----
result = con.select(select="*", table="sample_table")
print(con.get_attribute_name_list("sample_table"))
result = con.select(select="*", table_name="sample_table")
for record in result.fetchall():
print record
print(record)
# display type for each column -----
query = "SELECT DISTINCT TYPEOF(attr_a),TYPEOF(attr_b),TYPEOF(attr_c),TYPEOF(attr_d),TYPEOF(attr_e) FROM sample_table"
result = con.execute_query(query)
print result.fetchall()
# display data type for each column -----
print(con.get_attribute_type_list(table_name="sample_table"))
.. code:: console
['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')]
(u'integer', u'real', u'text', u'real', u'text')
Create table from csv file
~~~~~~~~~~~~~~~~~~~~~~~~~~

Input: sample\_data.csv
^^^^^^^^^^^^^^^^^^^^^^^

.. code:: csv
insert
------
"attr_a","attr_b","attr_c"
1,4,"a"
2,2.1,"bb"
3,120.9,"ccc"
Dictionary
~~~~~~~~~~
Example
^^^^^^^

.. code:: python
from simplesqlite import SimpleSQLite
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_from_csv(csv_path="sample_data.csv")
print(con.get_attribute_name_list("sample_data"))
result = con.select(select="*", table_name="sample_data")
for record in result.fetchall():
print(record)
Output of example
^^^^^^^^^^^^^^^^^

.. code:: console
['attr_a', 'attr_b', 'attr_c']
(1, 4.0, u'a')
(2, 2.1, u'bb')
(3, 120.9, u'ccc')
Insert records
--------------

Insert dictionary
~~~~~~~~~~~~~~~~~

.. code:: python
from simplesqlite import SimpleSQLite
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_with_data(
table_name="sample_table",
attribute_name_list=["attr_a", "attr_b", "attr_c", "attr_d", "attr_e"],
data_matrix=[[1, 1.1, "aaa", 1, 1]])
con.insert(
"sample_table",
{
table_name="sample_table",
insert_record={
"attr_a": 4,
"attr_b": 4.4,
"attr_c": "ddd",
Expand All @@ -93,8 +139,8 @@ Dictionary
}
)
con.insert_many(
"sample_table",
[
table_name="sample_table",
insert_record_list=[
{
"attr_a": 5,
"attr_b": 5.5,
Expand All @@ -108,50 +154,53 @@ Dictionary
},
]
)
result = con.select(select="*", table="sample_table")
result = con.select(select="*", table_name="sample_table")
for record in result.fetchall():
print record
print(record)
.. code:: console
(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')
(1, 1.1, u'aaa', 1, 1)
(4, 4.4, u'ddd', 4.44, u'hoge')
(5, 5.5, u'eee', 5.55, u'foo')
(6, u'NULL', u'fff', u'NULL', u'NULL')
list/tuple/namedtuple
~~~~~~~~~~~~~~~~~~~~~
Insert list/tuple/namedtuple
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python
from collections import namedtuple
from simplesqlite import SimpleSQLite
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_with_data(
table_name="sample_table",
attribute_name_list=["attr_a", "attr_b", "attr_c", "attr_d", "attr_e"],
data_matrix=[[1, 1.1, "aaa", 1, 1]])
SampleTuple = namedtuple(
"SampleTuple", "attr_a attr_b attr_c attr_d attr_e")
con.insert("sample_table", [7, 7.7, "fff", 7.77, "bar"])
con.insert(
table_name="sample_table",
insert_record=[7, 7.7, "fff", 7.77, "bar"])
con.insert_many(
"sample_table",
[
table_name="sample_table",
insert_record_list=[
(8, 8.8, "ggg", 8.88, "foobar"),
SampleTuple(9, 9.9, "ggg", 9.99, "hogehoge"),
]
)
result = con.select(select="*", table="sample_table")
result = con.select(select="*", table_name="sample_table")
for record in result.fetchall():
print record
print(record)
.. code:: console
(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')
(4, 4.4, u'ddd', 4.44, u'hoge')
(5, 5.5, u'eee', 5.55, u'foo')
(6, u'NULL', u'fff', u'NULL', u'NULL')
(1, 1.1, u'aaa', 1, 1)
(7, 7.7, u'fff', 7.77, u'bar')
(8, 8.8, u'ggg', 8.88, u'foobar')
(9, 9.9, u'ggg', 9.99, u'hogehoge')
Expand Down
4 changes: 2 additions & 2 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DataProperty>=0.2.0
six<=1.8.0
DataProperty>=0.2.1
six
Loading

0 comments on commit a8a4cc3

Please sign in to comment.