Skip to content

Instantly share code, notes, and snippets.

@guille150
Created March 4, 2014 17:50
Show Gist options
  • Save guille150/9351857 to your computer and use it in GitHub Desktop.
Save guille150/9351857 to your computer and use it in GitHub Desktop.
Importar script Sqlite Android
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
public class AssetDatabaseOpenHelper {
private static final String DB_NAME = "asset.db";
private Context context;
public AssetDatabaseOpenHelper(Context context) {
this.context = context;
}
public SQLiteDatabase openDatabase() {
File dbFile = context.getDatabasePath(DB_NAME);
if (!dbFile.exists()) {
try {
copyDatabase(dbFile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}
private void copyDatabase(File dbFile) throws IOException {
InputStream is = context.getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
os.flush();
os.close();
is.close();
}
}
// En clase Principal se hace un objeto y se llama a ella en el método onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AssetDatabaseOpenHelper db = adb.openDatabase();
Cursor c = db.rawQuery("SELECT * FROM Estadios;", null);
Vector<String> result = new Vector<String>();
while (c.moveToNext()){
Log.i("Resultados", "cnt: "+c.getInt(0)+" " +c.getString(1)+" " +c.getString(2)+" " +c.getInt(3)+" " +c.getString(4));
result.add(c.getInt(0)+" " +c.getString(1));
}
Log.i("Total Filas", "Filas: "+c.getCount());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment