Skip to content

Commit 613518a

Browse files
committed
corrigindo update
2 parents df3f0f2 + 6514a9f commit 613518a

10 files changed

Lines changed: 399 additions & 255 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 104 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
11
# SimpleSQL
22

3-
Essa Biblioteca tem como maior função faciliar o uso do SQLite para o android.
4-
5-
Agora vamos mostrar passo a passo de como usar:
6-
7-
### Passo 1: Criar uma classe herdando SQLiteOpenHelper
8-
9-
```JAVA
10-
public class HelperBD extends SQLiteOpenHelper {
11-
static final int DATABASE_VERSION = 1;
12-
static final String DATABASE_NAME = "example.db";
13-
Context context;
14-
15-
public HelperBD(Context context) {
16-
super(context, DATABASE_NAME, null, DATABASE_VERSION);
17-
this.context = context;
18-
}
19-
20-
@Override
21-
public void onCreate(SQLiteDatabase sqLiteDatabase) {
3+
Essa Biblioteca tem como maior função facilitar o uso do SQLite para o android.
4+
5+
Agora vamos mostrar o passo a passo de como utilizar:
6+
### Versões
7+
<a href="">v1.0.0</a>
8+
<a href="">v1.0.1</a>
9+
<a href="">v1.0.2</a>
10+
<a href="">v1.0.3</a>
11+
<a href="">v1.0.4</a>
12+
<a href="">v1.0.5</a>
13+
14+
### Importando a lib para o projeto:
15+
```groovy
16+
implementation 'com.github.p2jorg:simplesql:1.0.5'
17+
```
18+
##### *Observação - Caso você não tenha o JitPack, adicione em seu module project a linha de código com o comentário:
19+
```groovy
20+
allprojects {
21+
repositories {
22+
google()
23+
jcenter()
24+
maven { url "https://jitpack.io" } // JitPack
2225
2326
}
24-
25-
@Override
26-
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
27-
onCreate(sqLiteDatabase);
28-
}
29-
30-
@Override
31-
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
32-
super.onDowngrade(db, oldVersion, newVersion);
33-
onUpgrade(db, oldVersion, newVersion);
34-
}
3527
}
3628
```
37-
38-
### Passo 2: Crie sua classe modelo
39-
40-
<blockquote>
41-
<p>
42-
Preferimos usar a própria classe que vai servir pra passar os dados
43-
</p>
44-
</blockquote>
45-
29+
### Passo 1: Crie sua classe modelo
30+
Utilizando a biblioteca, a sua classe modelo também é a sua tabela de banco de dados,
31+
basta você utilizar as anotações necessárias para que as duas se tornem uma só.
4632
```JAVA
4733
@Table
4834
public class Pessoa {
@@ -86,70 +72,99 @@ public class Pessoa {
8672

8773
```
8874

89-
### Passo 3: Crie sua Tabela
75+
### Passo 2: Criar uma classe herdando SQLiteOpenHelper
76+
O Processo inicial de criar um banco de dados continua o mesmo, porém, como já foi visto anteriormente, a sua tabela já foi criada, então o que você precisar fazer é apenas chamar o um método da classe SimpleSQL dentro do método onCreate(SQLiteDatabase sqlLiteDatabase)
9077

9178
```JAVA
79+
public class HelperBD extends SQLiteOpenHelper {
80+
private static final int DATABASE_VERSION = 1;
81+
private static final String DATABASE_NAME = "example.db";
82+
Context context;
83+
private SimpleSQL simpleSQL;
84+
85+
public HelperBD(Context context) {
86+
super(context, DATABASE_NAME, null, DATABASE_VERSION);
87+
this.context = context;
88+
simpleSQL = new SimpleSQL(this);
89+
}
90+
91+
@Override
92+
public void onCreate(SQLiteDatabase sqLiteDatabase) {
93+
String _return = simpleSQL.create(new Pessoa(),db);
94+
}
95+
96+
@Override
97+
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
98+
String _return = simpleSQL.deleteTable(new Pessoa(),db);
99+
onCreate(db);
100+
}
101+
102+
@Override
103+
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
104+
super.onDowngrade(db, oldVersion, newVersion);
105+
onUpgrade(db, oldVersion, newVersion);
106+
}
107+
}
108+
```
109+
### Deletar a tabela
110+
```JAVA
92111
@Override
93-
public void onCreate(SQLiteDatabase sqLiteDatabase) {
94-
try {
95-
sqLiteDatabase.execSQL(SampleSQL.create(new Pessoa()));
96-
} catch (Exception e) {
97-
e.printStackTrace();
98-
}
112+
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
113+
String _return = simpleSQL.deleteTable(new Pessoa(),db);
114+
onCreate(sqLiteDatabase);
99115
}
100116
```
101-
102-
### Exemplo de como inserir dados na sua Tabela
103-
117+
### INSERT
118+
O método insert() irá retornar um valor booleano, onde true é quando for inserido com sucesso e false quando ocorrer algum erro
104119
```JAVA
105120
Pessoa p = new Pessoa();
106121
p.setNome("Alow");
107122
p.setIdade(12);
108123
boolean result = false;
109-
try {
110-
result = new SampleSQL(new HelperBD(this)).insert(p);
111-
} catch (Throwable throwable) {
112-
throwable.printStackTrace();
113-
}
124+
result = new SimpleSQL(new HelperBD(this)).insert(p);
114125
```
115126

116-
### Exemplo de como lista os dados da usa Tabela
117-
118-
```JAVA
119-
SampleSQL sampleSql = new SampleSQL(new HelperBD(this));
120-
List<Pessoa> pessoa2 = sampleSql.selectTable(new Pessoa())
121-
.where()
122-
.equals()
123-
.fields(new String[]{"*"})
124-
.execute();
125-
```
126-
127-
### Caso seja necessário atualizar o banco, faça da seguinte forma:
128-
129-
<blockquote>
130-
<p>
131-
Atualizar a versão do Banco
132-
</p>
133-
</blockquote>
134-
127+
### SELECT
128+
Para fazer uma listagem dos registro do banco de dados é bem simples, é só criar ua instancia da classe SimpleSQL e utilizar o método selectTable(Objeto) e montar o select da forma que preferir.
135129
```JAVA
136-
static final int DATABASE_VERSION = 2;
130+
SimpleSQL simpleSql = new SimpleSQL(new HelperBD(this));
131+
132+
List<Pessoa> list = simpleSQL.selectTable(new Pessoa())
133+
.fields(new String[]{"*"})
134+
.where()
135+
.collumn("id")
136+
.equals()
137+
.fieldInt(1)
138+
.execute();
139+
137140
```
141+
### DELETE
142+
Para remover algum registro da tabela, ainda segue o mesmo padrão dos métodos anteriores
143+
```JAVA
144+
boolean result = simpleSQL.deleteColumn(new Pessoa())
145+
.where()
146+
.column("id")
147+
.equals()
148+
.fieldInt(1)
149+
.execute();
138150

139-
<blockquote>
140-
<p>
141-
Deletar sua tabela para ela possa receber ou remover algum dado
142-
</p>
143-
</blockquote>
144-
151+
```
152+
### UPDATE
153+
Ainda utilizando o mesmo padrão dos anteriores você também pode atualizar os registro do banco de dados, da seguinte forma:
145154
```JAVA
146-
@Override
147-
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
148-
try {
149-
SampleSQL.deleteTable(new Pessoa());
150-
} catch (SQLException e) {
151-
e.printStackTrace();
152-
}
153-
onCreate(sqLiteDatabase);
154-
}
155+
SimpleSQL simpleSql = new SimpleSQL(new HelperBD(this));
156+
boolean result = simpleSQL.updateTable(new Pessoa())
157+
.set(new String[]{"nome","idade"})
158+
.values(new String[]{"Novo Nome","Nova Idade"})
159+
.where()
160+
.collumn("id")
161+
.equals()
162+
.fieldInt(1)
163+
.execute()
164+
155165
```
166+
167+
# Desenvolvedores
168+
<a href="https://github.com/PauloYR">Paulo Iury<a>
169+
<a href="https://github.com/LukNasc">Lucas Nascimento<a>
170+
<a href="https://github.com/jisellevms">Jiselle Martins<a>

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="br.com.sql">
4-
54
<!-- Required to act as a custom watch face. -->
6-
<uses-permission android:name="android.permission.WAKE_LOCK" />
7-
8-
<!-- Required for complications to receive complication data and open the provider chooser. -->
5+
<uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- Required for complications to receive complication data and open the provider chooser. -->
96
<uses-permission android:name="com.google.android.wearable.permission.RECEIVE_COMPLICATION_DATA" />
107

118
<application
@@ -14,6 +11,17 @@
1411
android:label="@string/app_name"
1512
android:roundIcon="@mipmap/ic_launcher_round"
1613
android:supportsRtl="true"
17-
android:theme="@style/AppTheme"></application>
14+
android:theme="@style/AppTheme">
15+
<activity
16+
android:name=".Example"
17+
android:screenOrientation="portrait"
18+
>
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
</application>
1826

1927
</manifest>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package br.com.sql;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import android.os.Bundle;
6+
7+
import com.simplesql.simplesql.config.SimpleSQL;
8+
9+
import java.sql.SQLException;
10+
import java.util.List;
11+
12+
public class Example extends AppCompatActivity {
13+
14+
@Override
15+
protected void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
setContentView(R.layout.activity_example);
18+
SimpleSQL simpleSQL = new SimpleSQL(new HelperBD(this));
19+
Pessoa pessoa = new Pessoa();
20+
pessoa.setName("paulo");
21+
simpleSQL.insert(pessoa);
22+
23+
List<Pessoa> list = simpleSQL.selectTable(new Pessoa())
24+
.fields(new String[]{"name"})
25+
.execute();
26+
27+
}
28+
}

app/src/main/java/br/com/sql/HelperBD.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,33 @@
1212

1313
/**
1414
* Created by Lucas Nascimento on 18/09/2019
15-
* Copyright (c) 2019 GFX Consultoria
15+
* Copyright (c) 2019 P2J
1616
*/
1717
public class HelperBD extends SQLiteOpenHelper {
1818
private static final String NAME = "nome_banco.bd";
19-
private static final int VERSION = 1;
19+
private static final int VERSION = 11;
20+
private SimpleSQL simpleSQL;
21+
2022
public HelperBD(@Nullable Context context) {
2123
super(context, NAME, null, VERSION);
24+
simpleSQL = new SimpleSQL(this);
2225
}
2326

2427
@Override
25-
public void onCreate(SQLiteDatabase sqLiteDatabase) {
26-
SimpleSQL simpleSQL = new SimpleSQL(this);
27-
try {
28-
simpleSQL.create(new Pessoa());
29-
} catch (SQLException e) {
30-
e.printStackTrace();
31-
}
32-
28+
public void onCreate(SQLiteDatabase db) {
29+
String _return = simpleSQL.create(new Pessoa(), db);
3330
}
3431

3532
@Override
36-
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
37-
SimpleSQL simpleSQL = new SimpleSQL(this);
38-
try {
39-
simpleSQL.deleteTable(new Pessoa());
40-
} catch (SQLException e) {
41-
e.printStackTrace();
42-
}
33+
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
34+
String _retorn = simpleSQL.deleteTable(new Pessoa(), db);
35+
onCreate(db);
36+
}
4337

38+
@Override
39+
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
40+
super.onDowngrade(db, oldVersion, newVersion);
41+
onUpgrade(db, oldVersion, newVersion);
4442
}
43+
4544
}
Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
package br.com.sql;
22

3-
/**
4-
* Created by Lucas Nascimento on 18/09/2019
5-
* Copyright (c) 2019 GFX Consultoria
6-
*/
3+
import com.simplesql.simplesql.annotations.AutoIncrement;
4+
import com.simplesql.simplesql.annotations.Column;
5+
import com.simplesql.simplesql.annotations.Key;
6+
import com.simplesql.simplesql.annotations.Table;
7+
8+
@Table
79
public class Pessoa {
10+
@AutoIncrement
11+
@Key
12+
@Column(type = "INTEGER")
13+
private int id;
14+
15+
@Column(type = "TEXT")
16+
private String name;
17+
18+
public int getId() {
19+
return id;
20+
}
21+
22+
public void setId(int id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
833
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".Example">
8+
9+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)