http://www.basef.com.br/index.php?title=Lendo_banco_de_dados_SQLite_no_Android&feed=atom&action=history
Lendo banco de dados SQLite no Android - Histórico de revisão
2024-03-28T15:16:59Z
Histórico de revisões para esta página neste wiki
MediaWiki 1.26alpha
http://www.basef.com.br/index.php?title=Lendo_banco_de_dados_SQLite_no_Android&diff=170&oldid=prev
Admin em 11h06min de 27 de abril de 2015
2015-04-27T11:06:34Z
<p></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr style='vertical-align: top;' lang='pt-BR'>
<td colspan='2' style="background-color: white; color:black; text-align: center;">← Edição anterior</td>
<td colspan='2' style="background-color: white; color:black; text-align: center;">Edição das 11h06min de 27 de abril de 2015</td>
</tr><tr><td colspan="2" class="diff-lineno" id="L159" >Linha 159:</td>
<td colspan="2" class="diff-lineno">Linha 159:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div></source></div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div></source></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>NOTA: O plugin do Firefox SQLite Manager ou o aplicativo SQLite Database Browser são dois bons aplicativos gerenciadores de banco de dados SQLite, ambos gratuitos.</div></td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">'''</ins>NOTA<ins class="diffchange diffchange-inline">'''</ins>: O plugin do Firefox SQLite Manager ou o aplicativo SQLite Database Browser são dois bons aplicativos gerenciadores de banco de dados SQLite, ambos gratuitos.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>FONTE: Conteúdo original deste artigo em Using your own SQLite database in Android applications.</div></td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">'''</ins>FONTE<ins class="diffchange diffchange-inline">'''</ins>: Conteúdo original deste artigo em <ins class="diffchange diffchange-inline">[http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ </ins>Using your own SQLite database in Android applications<ins class="diffchange diffchange-inline">]</ins>.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>[[<del class="diffchange diffchange-inline">Category</del>:Android]</div></td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[[<ins class="diffchange diffchange-inline">Categoria</ins>:Android<ins class="diffchange diffchange-inline">]</ins>]</div></td></tr>
</table>
Admin
http://www.basef.com.br/index.php?title=Lendo_banco_de_dados_SQLite_no_Android&diff=169&oldid=prev
Admin: Criou página com 'Caso um banco de dados SQLite já exista, para usá-lo num aplicativo Android: a) Criar a classe DataBaseHelper: <source lang="java" lines> package fhu.com.br; import java...'
2015-04-27T11:05:53Z
<p>Criou página com 'Caso um banco de dados SQLite já exista, para usá-lo num aplicativo Android: a) Criar a classe DataBaseHelper: <source lang="java" lines> package fhu.com.br; import java...'</p>
<p><b>Página nova</b></p><div>Caso um banco de dados SQLite já exista, para usá-lo num aplicativo Android:<br />
<br />
a) Criar a classe DataBaseHelper:<br />
<br />
<source lang="java" lines><br />
package fhu.com.br;<br />
<br />
import java.io.FileOutputStream;<br />
import java.io.IOException;<br />
import java.io.InputStream;<br />
import java.io.OutputStream;<br />
<br />
import android.content.Context;<br />
import android.database.SQLException;<br />
import android.database.sqlite.SQLiteDatabase;<br />
import android.database.sqlite.SQLiteException;<br />
import android.database.sqlite.SQLiteOpenHelper;<br />
<br />
public class DataBaseHelper extends SQLiteOpenHelper{<br />
<br />
// Android's folder for all your databases. You only have to change<br />
// "fhu.com.br" to match yours<br />
private static String DB_PATH = "/data/data/fhu.com.br/databases/";<br />
<br />
// Name of the database file<br />
private static String DB_NAME = "nomebanco.sqlite";<br />
<br />
// Internal variables<br />
private SQLiteDatabase dbHandle; <br />
private final Context dbContext;<br />
<br />
/**<br />
* Constructor<br />
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.<br />
* @param context<br />
*/<br />
public DataBaseHelper(Context context) {<br />
super(context, DB_NAME, null, 1);<br />
this.dbContext = context;<br />
} <br />
<br />
/**<br />
* Creates a empty database on the system and rewrites it with your own database.<br />
* */<br />
public void createDataBase() throws IOException {<br />
<br />
// Verifies if the database already exist<br />
boolean dbExist = checkDataBase();<br />
<br />
if (!dbExist) {<br />
<br />
// By calling this method and empty database will be created into the default system path<br />
// of your application so we are gonna be able to overwrite that database with our database.<br />
this.getReadableDatabase();<br />
<br />
try {<br />
this.copyDataBase();<br />
} catch (IOException e) {<br />
throw new Error("Erro ao copiar banco de dados");<br />
}<br />
}<br />
}<br />
<br />
/**<br />
* Check if the database already exist to avoid re-copying the file each time you open the application.<br />
* @return true if it exists, false if it doesn't<br />
*/<br />
private boolean checkDataBase(){<br />
<br />
SQLiteDatabase checkDB = null;<br />
<br />
try {<br />
String myPath = DB_PATH + DB_NAME;<br />
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);<br />
} catch (SQLiteException e) {<br />
// database does't exist yet.<br />
}<br />
<br />
if (checkDB != null) {<br />
checkDB.close();<br />
}<br />
<br />
return checkDB != null ? true : false;<br />
}<br />
<br />
/**<br />
* Copies your database from your local assets-folder to the just created empty database in the<br />
* system folder, from where it can be accessed and handled.<br />
* This is done by transfering bytestream.<br />
* */<br />
private void copyDataBase() throws IOException{<br />
<br />
// Openn local db as the input stream<br />
InputStream myInput = dbContext.getAssets().open(DB_NAME);<br />
<br />
// Path to the just created empty db<br />
String outFileName = DB_PATH + DB_NAME;<br />
<br />
// Opens the empty db as the output stream<br />
OutputStream myOutput = new FileOutputStream(outFileName);<br />
<br />
// Transfers bytes from the inputfile to the outputfile<br />
byte[] buffer = new byte[1024];<br />
int length;<br />
while ((length = myInput.read(buffer))>0){<br />
myOutput.write(buffer, 0, length);<br />
}<br />
<br />
// Closes the streams<br />
myOutput.flush();<br />
myOutput.close();<br />
myInput.close();<br />
<br />
}<br />
<br />
public void openDataBase() throws SQLException{<br />
// Open the database<br />
String myPath = DB_PATH + DB_NAME;<br />
dbHandle = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);<br />
}<br />
<br />
@Override<br />
public synchronized void close() {<br />
if (dbHandle != null)<br />
dbHandle.close();<br />
super.close();<br />
}<br />
<br />
@Override<br />
public void onCreate(SQLiteDatabase dbHandle) {<br />
<br />
}<br />
<br />
@Override<br />
public void onUpgrade(SQLiteDatabase dbHandle, int oldVersion, int newVersion) {<br />
<br />
}<br />
}<br />
</source><br />
<br />
b) Copiar o arquivo nomebanco.sqlite para o folder /assets/ do projeto Android.<br />
<br />
c) Na classe principal do aplicativo (Activity):<br />
<source lang="java" lines><br />
DataBaseHelper myDbHelper;<br />
myDbHelper = new DataBaseHelper(this);<br />
<br />
try {<br />
myDbHelper.createDataBase();<br />
} catch (IOException ioe) {<br />
throw new Error("Não foi possível criar o banco de dados.");<br />
}<br />
<br />
try {<br />
myDbHelper.openDataBase();<br />
} catch (SQLException sqle){<br />
throw sqle;<br />
}<br />
</source><br />
<br />
NOTA: O plugin do Firefox SQLite Manager ou o aplicativo SQLite Database Browser são dois bons aplicativos gerenciadores de banco de dados SQLite, ambos gratuitos.<br />
<br />
FONTE: Conteúdo original deste artigo em Using your own SQLite database in Android applications.<br />
<br />
[[Category:Android]</div>
Admin