Wednesday, April 27, 2011

Working with SQLite in AIR: Part 4

Today we continue learning SQL.

You can remove data from tables using the "DELETE" command. You need to specify the table's name and an expression for the "WHERE" operator. It will look like this:

DELETE FROM tablename WHERE expression

The "WHERE" command is not necessary in "DELETE", unlike in "UPDATE". If you don't use "WHERE", then you remove all the data in the table. If you only need to remove one or multiple rows that meet a specific criteria, use "WHERE". For example, we can remove the row which has the id of 1.

DELETE FROM contacts WHERE id = 1

Just like in "UPDATE", we can use "AND" and "OR" operators in the "WHERE" expression:

DELETE FROM contacts WHERE fname = "Bob" AND lname = "Johnson"

Now we know how to insert, update and remove data from the database. Next step - reading the data from the database.

Reading data from the table can be done in any ways, and it all depends on what you need. For example, you might want to read the whole table, or you might only want to read one column of each row.

To read something, you first need to select it. You can select values using the "SELECT" command. It looks like this:

SELECT column(, column2, column3...) FROM tablename

You can select all the columns in your table by putting "*" in the place where you would write your column name(s).

SELECT * FROM contacts

It is better to manually write down the columns, though. There are two reasons for that: one - the engine will process it faster that way, and two - you can control the order of them.

Select multiple columns like this:

SELECT fname, lname FROM contacts

You can also use the "WHERE" expression with the "SELECT" command.

SELECT fname, lname FROM contacts WHERE age = 25

That's all for now.

Thanks for reading!

Related:

Working with SQLite in AIR: Part 1
Working with SQLite in AIR: Part 2
Working with SQLite in AIR: Part 3
Working with SQLite in AIR: Part 5
Working with SQLite in AIR: Part 6
Working with SQLite in AIR: Part 7
Working with SQLite in AIR: Part 8
Working with SQLite in AIR: Part 9
Working with SQLite in AIR: Part 10
Working with SQLite in AIR: Part 11
Working with SQLite in AIR: Part 12

No comments:

Post a Comment