Android bundle sqlite database on application. Create sqlite database on first application invoke.
Tech-Today

Android bundle sqlite database on application. Create sqlite database on first application invoke.


How to setup a default sqlite database structure with data in an android application.

Add this class and initialize it in your main class that extends Activity.


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataHelper {
private static final String DATABASE_NAME = "kurenai.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_KURENAI = "kurenai";
private Context context;

public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
openHelper.getWritableDatabase();
}

private class OpenHelper extends SQLiteOpenHelper {

public OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String sql = String.format("SELECT name FROM sqlite_master WHERE type='table' AND name='%s'", TABLE_KURENAI);
Cursor c = db.rawQuery(sql, null);
try {
if(c.getCount() == 0) {
sql = "CREATE TABLE " + TABLE_KURENAI + " (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)";
db.execSQL(sql);
}
} finally {
c.close();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Sqlite", "Upgrading database, this process will destroy all data.");
onCreate(db);
}

}
}


How to call in your main class

public class SqliteDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataHelper dh = new DataHelper(this);
}
}




- Do Some Initialization Work After The Web Application Initialization Process
We can achieve this by using a webListener. See code below: package com.czetsuya; import java.text.MessageFormat; import java.util.ResourceBundle; import javax.inject.Inject; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;...

- Create An Iphone Application Using Xcode That Dynamically Creates Sqlite Tables
This tutorial will try to create an iphone application that will dynamically create tables in sqlite database. This tutorial will use anime information for example. Note that this tutorial is not for beginner, I'll just summarized the steps and provide...

- Loading Locale From Post/get Parameter
This page will summarize how to load a resource bundle dynamically from a GET/POST parameter.  1.) In your mail xhtml template, make sure that you define f:view locale before the h:body tag: <f:view locale="#{languageBean.currentLocale}" />...

- Implement A Stored Procedure In Entity Framework
Implement a stored procedure in Entity Framework 1.) To do this we have to create a database (name according to your preference) and add the following tables: CREATE TABLE [dbo].[Authors]( [AuthorID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](100)...

- Android's Datepickerdialog And Timepickerdialog Implementation
Layout (calendar.xml) Java Class: package org.ipiel.demo.layout; import java.text.DateFormat; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.os.Bundle;...



Tech-Today








.