librelist archives

« back to archive

why my query from sqlite3 returns long type

why my query from sqlite3 returns long type

From:
Alex
Date:
2011-05-23 @ 15:50
This is how i created the table entries:

> create table entries (
>        id integer primary key autoincrement,
>        title string not null,
>        text string not null
> );
>

How i query the database:

> @app.route('/')
>
def show_entries():
>     cur = g.db.execute('select id, title, text from entries order by id
> desc')
>     *entries* = [dict(id=row[0], title=row[1], text=row[2]) for row in
> cur.fetchall()]
>     return render_template('show_entries.html', *entries*=*entries*)
>

Add an entry:

> @app.route('/add', methods=['POST'])
> def add_entry():
>     if not session.get('logged_in'):
>         abort(401)
>     g.db.execute('insert into entries (title, text) values (?, ?)',
>                  [request.form['*title*'], request.form['*text*']])
>     g.db.commit()
>     flash('New entry was successfully posted')
>     return redirect(url_for('show_entries'))
>

The template *show_entries.html*:

> {% extends "layout.html" %}
> {% block body %}
> <ul class=entries>
>     {% for entry in entries %}
>     <li><h2>{{ entry.title }}</h2>
>     {% if session.logged_in %}
>     <span class=control><a href={{ url_for('del_entry', id=entry.id)
> }}>Delete</a></span>
>     {% endif %}
>     {{ *entry.**text*|safe }}
>     {% else %}
>     <li><em>Unbelievable.  No entries here so far</em>
>     {% endfor %}
> </ul>
> {% if session.logged_in %}
> <form action="{{ url_for('add_entry') }}" method=post class=add-entry>
>     <dl>
>         <dt>Name:
>         <dd><input type=text size=30 name=*title*>
>         <dt>Phone Number:
>         <dd><input type=text size=30 name=*text*>
>         <!--<textarea name=text rows=5 cols=40></textarea>-->
>         <dd><input type=submit value=Add>
>     </dl>
> </form>
> {% endif %}
> {% endblock %}
>

When i input '13320943021' for *text*, the type of *entry.text* is long. The
table is created as string type. Why the result of query is long?
Thanks.

Re: [flask] why my query from sqlite3 returns long type

From:
Ron DuPlain
Date:
2011-05-23 @ 16:41
On Mon, May 23, 2011 at 11:50 AM, Alex <gfreezy@gmail.com> wrote:
> This is how i created the table entries:
>>
>> create table entries (
>>        id integer primary key autoincrement,
>>        title string not null,
>>        text string not null
>> );
...
> When i input '13320943021' for text, the type of entry.text is long. The
> table is created as string type. Why the result of query is long?

Values are dynamically typed in SQLite.  You get a type based on the
value, not the schema.

http://www.sqlite.org/datatype3.html

"SQLite uses a more general dynamic type system. In SQLite, the
datatype of a value is associated with the value itself, not with its
container. The dynamic type system of SQLite is backwards compatible
with the more common static type systems of other database engines in
the sense that SQL statement that work on statically typed databases
should work the same way in SQLite. However, the dynamic typing in
SQLite allows it to do things which are not possible in traditional
rigidly typed databases."

Hope this helps,

Ron

Re: [flask] why my query from sqlite3 returns long type

From:
Alex
Date:
2011-05-24 @ 02:49
Thank your for replying.
Is it possible to insert '13320943021' as a string or i have to convert it
to string after query.


2011/5/24 Ron DuPlain <ron.duplain@gmail.com>

> On Mon, May 23, 2011 at 11:50 AM, Alex <gfreezy@gmail.com> wrote:
> > This is how i created the table entries:
> >>
> >> create table entries (
> >>        id integer primary key autoincrement,
> >>        title string not null,
> >>        text string not null
> >> );
> ...
> > When i input '13320943021' for text, the type of entry.text is long. The
> > table is created as string type. Why the result of query is long?
>
> Values are dynamically typed in SQLite.  You get a type based on the
> value, not the schema.
>
> http://www.sqlite.org/datatype3.html
>
> "SQLite uses a more general dynamic type system. In SQLite, the
> datatype of a value is associated with the value itself, not with its
> container. The dynamic type system of SQLite is backwards compatible
> with the more common static type systems of other database engines in
> the sense that SQL statement that work on statically typed databases
> should work the same way in SQLite. However, the dynamic typing in
> SQLite allows it to do things which are not possible in traditional
> rigidly typed databases."
>
> Hope this helps,
>
> Ron
>

Re: [flask] why my query from sqlite3 returns long type

From:
Alex
Date:
2011-05-24 @ 03:04
The problem is solved. I change the schema to
 create table entries (
        id integer primary key autoincrement,
        title text not null,
        text text not null
  );
The type should be text.Now number is inserted as a string.
Thanks for the referring.

2011/5/24 Alex <gfreezy@gmail.com>

> Thank your for replying.
> Is it possible to insert '13320943021' as a string or i have to convert it
> to string after query.
>
>
>
> 2011/5/24 Ron DuPlain <ron.duplain@gmail.com>
>
>> On Mon, May 23, 2011 at 11:50 AM, Alex <gfreezy@gmail.com> wrote:
>> > This is how i created the table entries:
>> >>
>> >> create table entries (
>> >>        id integer primary key autoincrement,
>> >>        title string not null,
>> >>        text string not null
>> >> );
>> ...
>> > When i input '13320943021' for text, the type of entry.text is long. The
>> > table is created as string type. Why the result of query is long?
>>
>> Values are dynamically typed in SQLite.  You get a type based on the
>> value, not the schema.
>>
>> http://www.sqlite.org/datatype3.html
>>
>> "SQLite uses a more general dynamic type system. In SQLite, the
>> datatype of a value is associated with the value itself, not with its
>> container. The dynamic type system of SQLite is backwards compatible
>> with the more common static type systems of other database engines in
>> the sense that SQL statement that work on statically typed databases
>> should work the same way in SQLite. However, the dynamic typing in
>> SQLite allows it to do things which are not possible in traditional
>> rigidly typed databases."
>>
>> Hope this helps,
>>
>> Ron
>>
>
>