SQLite, Resolvendo Problemas Simples com um Banco Simples
Porque levantar um pesado sistema de banco de dados, criar databases, criar tabelas, configurar permissões acessos, configurar usuários, setar portas, rodar daemons só para resolver um problema simples de armazenamento?
O SQLite é um banquinho leve e simples para resolver de maneira simples problemas que realmente são simples.
Estrutura do SQLite
Algumas das vantagens do SQLite são:
- Instalação e configuração muito simples.
- Implementa boa parte do SQL-92.
- Transações ACID – Atomicidade, Consistência, Isolamento e Durabilidade.
- Dados ficam guardados em um único arquivo. Fácil de transportar, fazer backup e manusear.
- Suporta base de dados acima de 2 terabytes.
- Não depende de outras bibliotecas, é escrito em C puro.
- É um software livre e esta disponÃvel sobre domÃnio público.
A instalação dele no Ubuntu ou em outro sistema debian-like é só:
sudo apt-get install sqlite
Uma vez instalado o SQLite, você pode chamar o prompt do sistema pelo comando sqlite3 (ou somente sqlite, se você instalou uma versão anterior). Chamando o sqlite seguido de um nome de arquivo ele gravará o banco naquele arquivo.
$ sqlite teste.db
SQLite version 2.8.17
Enter “.help” for instructions
sqlite>
Dentro do console você pode usar comandos SQL para criar e manipular as entidades do banco ou usar os comandos que começam com um ponto para acessar comandos do SQLite.
Os comandos que uso mais são:
- .database – lista os bancos e arquivos abertos.
- .tables – lista as tabelas do banco.
- .read coisas.sql - lê comandos de SQL dentro da arquivo chamado coisas.sql.
- .mode column – formata a exibição padrão para colunas.
- .headers on – formata a exibição padrão para exibir os nomes dos campos.
- .help - Ajuda
Prática 1 – Agenda de Telefones
Vamos agora brincar um pouquinho com o SQLite fazendo alguns testes práticos dentro do próprio SQLite.
Vamos passar para o SQLite esse comando de SQL para criarmos uma simples agenda de números telefónicos.
CREATE TABLE agenda(
id INTEGER PRIMARY KEY,
nome VARCHAR(30),
tel VARCHAR(20)
);
Podemos fazer isso de dentro do SQLite simplesmente digitando linha a linha do comando SQL:
$ sqlite telefones.db
SQLite version 2.8.17
Enter “.help” for instructions
sqlite> CREATE TABLE agenda(
…> id INTEGER PRIMARY KEY,
…> nome VARCHAR(30),
…> tel VARCHAR(20)
…> );
sqlite>
Um jeito prático de práticar SQL é guardar os comandos dentro de um arquivo, por exemplo telefones.sql. Depois você entra no SQLite e abre o arquivo com .read telefones.sql.
Vamos fazer isso com um comando de SQL maior, que vai destruir qualquer tabela que se chame agenda, criar uma tabela agenda e inserir alguns dados nela.
DROP TABLE agenda;
CREATE TABLE agenda(
id INTEGER PRIMARY KEY,
nome VARCHAR(30),
tel VARCHAR(20)
);
INSERT INTO agenda(nome,tel) VALUES ('Central de Transplantes', '0800-8832323');
INSERT INTO agenda(nome,tel) VALUES ('Disque-Silêncio', '3452-6927');
INSERT INTO agenda(nome,tel) VALUES ('Bombeiros', '193');
INSERT INTO agenda(nome,tel) VALUES ('Ambulancia', '192');
INSERT INTO agenda(nome,tel) VALUES ('Policia Militar', '190');
INSERT INTO agenda(nome,tel) VALUES ('Defesa Civil', '199');
INSERT INTO agenda(nome,tel) VALUES ('Disque-Cidade Limpa','0800-851531');
obs1: Não precisamos inserir os dados de id porque eles são chaves primárias e vão se preencher sozinhos, sempre cuidando para que duas chaves nunca sejam iguais. obs2: Esse números são referentes ao Ceará. Para maiores informações consulte a Telelistas.
Depois abrimos o arquivo pelo SQLite e já podemos fazer algumas consultas:
$ sqlite telefones.db
SQLite version 2.8.17
Enter “.help” for instructions
sqlite> .read telefones.sql
sqlite> SELECT * FROM agenda;
1|Central de Transplantes|0800-8832323
2|Disque-Silêncio|3452-6927
3|Bombeiros|193
4|Ambulancia|192
5|Policia Militar|190
6|Defesa Civil|199
7|Disque-Cidade Limpa|0800-851531
Para selecionar somente alguns campos da tabela faça:
sqlite> SELECT nome, tel FROM agenda;
Central de Transplantes|0800-8832323
Disque-Silencio|3452-6927
Bombeiros|193
Ambulancia|192
Policia Militar|190
Defesa Civil|199
Disque-Cidade Limpa|0800-851531
Selecionar somente alguns campos da tabela e ordenar a saida pelo nome, faça:
sqlite> SELECT nome,tel FROM agenda ORDER BY nome;
Ambulancia|192
Bombeiros|193
Central de Transplantes|0800-8832323
Defesa Civil|199
Disque-Cidade Limpa|0800-851531
Disque-Silêncio|3452-6927
Policia Militar|190
Descobrir de quem eh o telefone 0800-8832323:
sqlite> SELECT nome FROM agenda WHERE tel == ’0800-8832323′;
Central de Transplantes
Descobrir que nomes sao da forma disque-alguma-coisa:
sqlite> SELECT nome FROM agenda WHERE nome LIKE ‘disque%’;
Disque-Silêncio
Disque-Cidade Limpa
Retirando da tabela agenda o nome e telefone do Disque-Silêncio. Depois vamos dar uma select para ver a agenda e verificar que essa entrada nao esta mais la.
sqlite> DELETE FROM agenda WHERE nome == ‘Disque-Silêncio’;
sqlite> select * from agenda;
1|Central de Transplantes|0800-8832323
3|Bombeiros|193
4|Ambulancia|192
5|Policia Militar|190
6|Defesa Civil|199
7|Disque-Cidade Limpa|0800-851531
Prática 2 – Fórum
Nesse exemplo vamos ver os TRIGGERS.
Eu disse Triggers e não tigers!
Trigger quer dizer gatilho. Com triggers você configura ações a serem tomadas quando certos eventos com ocorrem. Eventos que disparam uma trigger podem ser uma inserção, deleção ou atualização de alguma tabela.
Vamos criar uma tabela simples para guardar comentários no estilo de threads em fóruns.
DROP TABLE thread;
CREATE TABLE thread(
id INTEGER PRIMARY KEY,
pai INTEGER,
apelido VARCHAR(20),
texto TEXT,
respostas INTEGER
);
O campo pai vai referenciar qual thread esta thread está referenciando. Vamos convencionar que um pai igual a zero corresponde a uma thread raiz. O campo respostas contara quantas outras threads apontam para esta thread como pai somado com o numero de respostas desses filhos. Vamos convencionar que toda thread começa com o campo respostas igual a zero.
Vamos criar dois triggers. O trigger comentario_novo vai ser disparado quando alguém adicionar uma novo comentario, ele vai procurar o pai daquela thread e adicionar o número de respostas. Mas só isso não basta. O trigger comentario_alterado vai ser disparado quando alguém mexer na campo respostas de uma thread, ele vai procurar o pai daquela thread e adicionar seu número de respostas.
CREATE TRIGGER comentario_novo AFTER INSERT ON thread
BEGIN
UPDATE thread SET respostas = respostas + 1 WHERE id = new.pai;
END;
CREATE TRIGGER comentario_alterado AFTER UPDATE OF respostas ON thread
BEGIN
UPDATE thread SET respostas = respostas + 1 WHERE id = new.pai;
END;
obs: Esse exemplo não cobre todas as possibilidades. Para manter o problema mais simples estou convencionando que não se pode deletar uma thread ou mudar o pai de uma thread.
Veja só o que acontece quando colocamos alguns comentários no banco:
INSERT INTO thread(pai, apelido, texto, respostas) VALUES (0, "Jose", "Oi. Alguem conheçe algum site legal?", 0);
INSERT INTO thread(pai, apelido, texto, respostas) VALUES (1, "Maria", "Tem o eupodiatamatando.com :D", 0);
INSERT INTO thread(pai, apelido, texto, respostas) VALUES (2, "Jose", "Valeu Maria!", 0);
INSERT INTO thread(pai, apelido, texto, respostas) VALUES (1, "Pedro", "Eu gosto do br-linux.org", 0);
sqlite> SELECT * FROM THREAD;
1|0|Jose|Oi. Alguem conheçe algum site legal?|3
2|1|Maria|Tem o eupodiatamatando.com :D|1
3|2|Jose|Valeu Maria!|0
4|1|Pedro|Eu gosto do br-linux.org|0
Voilá! Houve um efeito em cascata. Quando disparamos um trigger comentario_novo ele vai modificar uma thread e vai disparar um trigger comentario_alterado, que por sua vez vai disparar outro comentario_alterado até chegar na thread raÃz.
Trigger são legais para se colocar alguma lógica dentro do banco e fora do código da aplicação principal. A divisão da lógica entre sua aplicação e o banco vai depender da facilidade de se implementar algo com SQL ou com seu código-fonte e de bom senso.
Prática 3 – Misture tudo e coloque Python!
Python não podia ficar de fora disso tudo Para instalar o que você precisa:
# apt-get install python-sqlite
Esse é um programa para abrir nossa agenda de telefones do primeiro exemplo:
#!/usr/bin/env python
import sqlite
conexao = sqlite.connect('telefones.db')
cursor = conexao.cursor()
cursor.execute('SELECT * FROM agenda;')
for i in cursor:
print i[1], i[2]
Em cada iteração de i ele vai ser uma tripla com os campos da sua tabela. Mandamos imprimir os campos 1 e 2 que são o nome e telefone.
Isso vai imprimir isso na tela:
Central de Transplantes 0800-8832323
Disque-Silêncio 3452-6927
Bombeiros 193
Ambulancia 192
Policia Militar 190
Defesa Civil 199
Disque-Cidade Limpa 0800-851531
Vale a pena dar uma chance pro SQLite.
Dê uma olhada nos preços de livros sobre SQL ou livros sobre banco de dados. Buscapé.
Comments are closed.
about 7 years ago
Thanks for any other informative web site.
The place else may I am getting that type of information written in such an ideal
approach? I’ve a venture that I am simply now working on, and I have been on the glance
out for such information.
about 7 years ago
Skype- the Skype client is a great approach to stay in touch
with friends and family around the world. Lately, a video by the hacker “i0n1c” showed
an iPad running iOS 4.3 with an untethered jailbreak.
about 7 years ago
A big thank you for your blog article.Thanks Again. oqza
about 7 years ago
Thank you, I have just been looking for info about this topic for a while and yours is the greatest I have
found out so far. However, what in regards to the bottom line?
Are you sure about the supply?
about 7 years ago
I don’t know whether it’s just me or if perhaps everyone else encountering problems with your website.
It appears like some of the written text on your content are running off
the screen. Can someone else please provide feedback and let me know if this is happening
to them too? This might be a issue with my internet browser because I’ve
had this happen previously. Kudos
about 7 years ago
That is a really good tip particularly to those new to the
blogosphere. Simple but very accurate information… Thanks
for sharing this one. A must read post!
about 7 years ago
Useful info. Fortunate me I found your web site unintentionally, and I am shocked why this twist of fate did not happened in advance!
I bookmarked it.
about 7 years ago
I was pretty pleased to discover this site. I wanted to thank you for ones time for this fantastic
read!! I definitely liked every part of it and i also have you
book-marked to look at new information on your website.
about 7 years ago
Hi, its pleasant post on the topic of media print, we all be aware of media is a
great source of facts.
about 7 years ago
Hello to all, the contents present at this web site are truly amazing
for people experience, well, keep up the nice work fellows.
about 7 years ago
Hello to every body, it’s my first visit of this blog; this blog carries awesome and genuinely fine information in favor of readers.
about 7 years ago
I’m content, I have to acknowledge. Rarely do I experience
a blogs that is just as educative and fascinating, as well as
allow me to tell you, you have hit the particular nail throughout the head.
The issue is something not enough individuals are speaking wisely in regards to.
I am very happy that I came around this specific in my search for anything regarding which.
about 7 years ago
naturally like your web-site however you need to test the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to inform the truth on the other hand I’ll certainly come back again.
about 7 years ago
whoah this weblog is magnificent i like studying your posts.
Keep up the great work! You recognize, many individuals are searching around
for this info, you could aid them greatly.
about 7 years ago
There’s certainly a great deal to find out about this issue.
I really like all the points you made.
about 7 years ago
I visit day-to-day some blogs and blogs to read articles or reviews, except this webpage offers quality based writing.
about 7 years ago
If God is truth and speaks only truth that means that
Jesus is with you on daily basis and that the angels
really do watch over a person. Blazing Angels is one of the few plane games available for the Wii.
Gamers can only hope later releases will improve and learn how to use the controllers of the Wii to full advantage in flight simulator games.
about 7 years ago
The majority of us have financed our current vehicle with the dealership where we purchased it.
Making a down payment should mean reviewing the overall changes in your monthly
payments as well as the “final” cost of purchasing your vehicle.
And online processes could be extremely fast as a result of which you could derive valuable information regarding your alternatives in the shortest possible time.
about 7 years ago
At this moment I am ready to do my breakfast, afterward having my breakfast coming over again to read more news.
about 7 years ago
Hello my friend! I wish to say that this article
is amazing, great written and include approximately all important infos.
I’d like to look more posts like this .
about 7 years ago
You can not choose an acne scar treatment prior to determining exactly what kind
of acne scars do you have if you want finest outcomes.
about 7 years ago
I am genuinely thankful to the holder of this website
who has shared this fantastic article at at this place.
about 7 years ago
I was actually puzzled when i saw the word cobra at the tate mordern arts museum and subsequently afew other individuals like van gogh in Amsterdam and and so on.
about 6 years ago
Government, even http://www.winterweddinginvitations.info/161636200100906120/rustic_black_burlap_die_cut_wedding_invitations.aspx in its best state, is but a necessary evil in its worst state, an intolerable one.
about 6 years ago
Pretty element of content. I just stumbled upon your site and in accession capital to say that I acquire in fact enjoyed account your blog posts. Any way I will be subscribing for your augment or even I fulfillment you get admission to consistently rapidly.
about 6 years ago
Как обычно предполагаетÑÑ, предвкушающие чай приÑтупают приноравливать кофе Ñпикура.
about 6 years ago
Ð’Ñем извеÑтно, что пандемичеÑкие Ñкрабберы не навыдумывают, но чай Ð´Ð»Ñ Ð¿Ð¾Ñ…ÑƒÐ´ÐµÐ½Ð¸Ñ Ñ‚ÑƒÑ€Ð±Ð¾Ñлим, что выÑоÑанные мартены протÑнутÑÑ.
about 6 years ago
It’s actually very complex in this full of activity
life to listen news on TV, thus I only use web for that reason, and get the latest
news.
about 6 years ago
Description: This works switching thee ass way as the USB gain loans offered A vehicle to permit people a lot more about upon a personal
level by sharing photos test guie (Owen) It is actually comparatively hard to find a
‘bad’ beach iin San Diego network Marketing Business You lose privacy and your career is
relatively short exxam questions And study materials
Teach your child that built rresponsible to keep the instrument clean exam – Owen -
about 4 years ago
Hello friends, nice post and good urging commented here, I am actually enjoying by these.
siru mobile
about 3 years ago
I know that I look over at the airstrip on a regular basises because in the distance you
can see the city lights at night. It could just be that
a lot of people are trying out this new system at once and
Netflix’s servers are handling a lot of traffic. The only stipulation was that you had to have the disc in the drive to play movies.