• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps SQLite databases are hard :(

Booocubs

Newbie
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 :(((((
 
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!
 
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/
 
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?
 
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.
 
Back
Top Bottom