Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old October 26th, 2011, 11:22 PM   #1 (permalink)
New Member
 
Join Date: Oct 2011
Posts: 8
 
Device(s):
Thanks: 6
Thanked 0 Times in 0 Posts
Default SQLite databases are hard :(

I've been going at a decent pace learning anrdoid programming and my first app is almost done... the last piece my app needs is...
A DATABASE!
It needs to
- Create the database when the app loads for the first time
- Store to the database when the edittext is entered
- Read from the database to display the previously entered edittext's

I'm having sooooooo much trouble wrapping my head around this whole database idea. It's so hard for me to learn, the context in most of the databases created in tutorials on youtube are hard to follow and don't explain what they're doing very well, any ideas on how to learn databases? I really want to finish this app but I need to get over this hump ((((

Booocubs is offline  
Reply With Quote
Sponsors
Old October 27th, 2011, 10:52 AM   #2 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 308
 
Device(s): Samsung Galaxy Nexus
Thanks: 0
Thanked 52 Times in 51 Posts
Default

Heres a nice tutorial: Android SQLite Database - Tutorial.
And if you got questions just fire away in this forum.
__________________
Android: Personal Storage, Lars Monsen Facts(Norwegian)
miXer is offline  
Reply With Quote
The Following 2 Users Say Thank You to miXer For This Useful Post:
alostpacket (November 1st, 2011), Booocubs (October 27th, 2011)
Old October 27th, 2011, 04:38 PM   #3 (permalink)
New Member
 
Join Date: Oct 2011
Posts: 8
 
Device(s):
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hey mixer, on your link, look at
"2.3 database handling"
the code

Private static final string DATABASE_CREATE = "create table todo (_id integer primary key autoincrement, "
+ "category text not null, summary text not null, description text not null);";

what is all that in the parenthesis saying? it's so confusing because I can't tell if those are commands, or a text output or anything. I'm so confused. Maybe you could clarify this snippet for me?
Thanks so much by the way, you've helped me on two or three topics so far and I greatly appreciate everything!
Booocubs is offline  
Reply With Quote
Old October 28th, 2011, 02:36 AM   #4 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

Quote:
Originally Posted by Booocubs View Post
private static final string DATABASE_CREATE = "create table todo (_id integer primary key autoincrement, "
+ "category text not null, summary text not null, description text not null);";
Let's start by reformatting that SQL statement` so it's easier to see the parts. I also capitalised the SQL keywords to distinguish them from your identifiers. SQL keywords are not case-sensitive (table is Table is TABLE), unlike Java.

Code:
CREATE TABLE todo
(
  _id INTEGER PRIMARY KEY AUTOINCREMENT,
  category TEXT NOT NULL,
  summary TEXT NOT NULL,
  description TEXT NOT NULL
);
This is an example of a CREATE TABLE statement. The full syntax and details of this statement in sqlite3 is documented here: SQLite Query Language: CREATE TABLE.

This example creates a table called todo.

The clauses between the parentheses describe the table. In this example you have 4 column definitions.

Each column definition starts with a column name. This is optionally followed by the name of a datatype. Finally there are zero or more column constraints.

The first clause defines a column called _id that holds integer values. There are two column constraints. First it defines the _id column to be the table's primary key. Next it defines that the column will auto increment.

The next three clauses define columns called category, summary and description. These columns are all defined as holding text. Each has a single column constraint, NOT NULL. This precludes any of these columns from being NULL. If you try to insert a row with null for any of these column, sqlite3 will raise an error.

Note that PRIMARY KEY implies NOT NULL. So the _id column is also precluded from storing NULL.


If you are new to SQL itself, maybe this online SQL tutorial might help. http://www.sqlcourse.com/
jiminaus is offline  
Last edited by jiminaus; October 28th, 2011 at 02:41 AM. Reason: Added SQL tutorial link
Reply With Quote
The Following 2 Users Say Thank You to jiminaus For This Useful Post:
Booocubs (October 31st, 2011), DoddyUK (November 15th, 2011)
Old October 31st, 2011, 10:19 PM   #5 (permalink)
New Member
 
Join Date: Oct 2011
Posts: 8
 
Device(s):
Thanks: 6
Thanked 0 Times in 0 Posts
Default

So Jiminaus, these constraints.

_id
category
summary
description

In the application I am making, (It's my first one so I am keeping it VERY basic...) Alls I have is a text box, and a button that inserts what is typed in the textbox into the listView below.

So when creating my table, what should my constraints be?
Booocubs is offline  
Reply With Quote
Old November 1st, 2011, 01:14 AM   #6 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

You mean column, not constraint. A database is made up of tables. Tables are made up of columns and rows. Constraints can be applied to tables and columns.

It looks like you'd only need the _id column and a text column to store the string from the text box.
jiminaus is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:10 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo