How to Create default database in SQ-Lite database? so if the Database/Value doesn't exist i want my App to use default Database/Value automatically..
Here is my database creation code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase("D5",MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Record(value1 VARCHAR,value2 VARCHAR,value3 VARCHAR);");
}
What i usually do is a class similar to this...
public class SQLiteAdapter extends SQLiteOpenHelper{
private static final int DATABASE_VERSION=1;
private static String DATABASE_NAME="your_database_name";
public SQLiteAdapter(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS Record(value1 VARCHAR,value2 VARCHAR,value3 VARCHAR);");
}
}
then when you want to use it from an activity:
SQLiteAdapter db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new SQLiteAdapter(this);
}
and with that instance db you can do whatever you want, db.execSQL, etc. etc.
Related
I write SQL code at small android application, always database create successfully, but that is not correct for the table.
public dbSqlHelper dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new dbSqlHelper(this);
SQLiteDatabase dataSql = dbHelper.getWritableDatabase();
}
that is the main, and the following code for dbhelper
private static final String DATABASE_NAME = "malekData.db";
private static final int DATABASE_VERSION = 1;
private Context context;
public dbSqlHelper(#Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate( SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE malekDB4 ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");
} catch (SQLException e) {
Log.e("Malek", "Error when creating table: " + e.getMessage());
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
when I go to terminal and check table (.tables), no thing return
I have veritabani.java and i create database in this class. Then i create object in mainactivity.java. But when i run app. the program dont move database.java so i cant create database how i can solve this problem.
veritabani.java
public class veritabani extends SQLiteOpenHelper {
private static final String VERİTABANİ_ADİ="kayit";
public veritabani(Context c)
{
super(c,VERİTABANİ_ADİ,null,2);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE kayit(ilacadi TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST kayit");
onCreate(db);
}
}
mainactivity.java
public class MainActivity extends Activity {
private veritabani v1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
v1=new veritabani(this);
}
v1=new veritabani(this) doesnt work.
Try this code
public class DBAdapter
{
private static final String DATABASE_NAME = "GSDATA.db";
private static final int DATABASE_VERSION = 2;
private static final String CREATE_COVERPHOTO_TABLE = "CREATE TABLE IF NOT EXISTS COVERPHOTO (path Text,date DATE);";
private final Context context;
public String group;
private DatabaseHelper DBHelper;
private static SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_COVERPHOTO_TABLE);
System.out.println("The Database is Created Here :");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// db.execSQL("DROP TABLE IF EXISTS calSimpleNote");
onCreate(db);
}
} // database helper class complete
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
db.close();
}
// and write below line in main activity
DBAdapter adapter = new DBAdapter(MainActivity.this);
I'm not sure what you mean by "the program dont move database.java" but i had issues with databases using SQLiteOpenHelper.
Actually, SQLiteOpenHelper calls " public void onCreate(SQLiteDatabase db)" only once, so if you change your tables between 2 launch, it won't be updated.
To solve this you have to manually delete the database by going into parameters / applications / [your_app] / erase data (or something like this) and try again.
Please why do I get the error "SQLiteException: no such table: tabella (code1)???
from LogCat I get: "SQLiteException: no such table: tabella (code1)" when I try to access the database fro the activity.
Here is the SQLiteOpenHelper code:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String CREATE_DB_SQL = "CREATE TABLE tabella (_id INTEGER PRIMARY KEY AUTOINCREMENT, colonna1 TEXT, colonna2 TEXT, colonna3 TEXT);";
public DatabaseHelper(Context context) {
super(context, "basedati", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_SQL);
ContentValues cv = new ContentValues (3);
cv.put("colonna1", "valore colonna 1");
cv.put("colonna2", "valore colonna 3");
cv.put("colonna3", "valore colonna 3");
db.insert("tabella", "colonna1", cv);
}
#Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS tabella");
onCreate(db);
}
}
Here is the Activity code in which I try to open the database:
public class MainActivity extends Activity {
private DatabaseHelper db=null;
private Cursor constantsCursor=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(this);
constantsCursor=db.getReadableDatabase().rawQuery("SELECT colonna1 FROM tabella ORDER BY colonna1", null);
}
}
Odds are that you have changed the table after running your app at least once. You cannot simply change the Java String CREATE_DB_SQL, you must also update the SQL schema. The easiest way to do this is to increment the version number:
public DatabaseHelper(Context context) {
super(context, "basedati", null, 2); // Change the number to two
}
Hi i am going to create a SQLite database and insert records. but my database does not create.
following is the code. Cant understand why. trying this for a long time
If someone can help its a great help
public class DatabaseHelper extends SQLiteOpenHelper{
static final String dbName="MyDatabase";
public DatabaseHelper(Context context) {
super(context, dbName, null,33);
}
#Override
public void onCreate(SQLiteDatabase db) {
String qry = "CREATE TABLE DEPT(dept_id INTEGER PRIMARY KEY, deptName TEXT)";
db.execSQL(qry);
}
}
I am calling the constructor like this
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHelper dp = new DatabaseHelper(this);
Toast.makeText(this, "Finish Execution", Toast.LENGTH_LONG).show();
}
}
create your database by using this sample
http://marakana.com/forums/android/examples/55.html
How to declare database in android? How we make a database in android and method?
Basically you extend a class with SQLiteOpenHelper and implement onCreate and onUpgrade.
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db_table";
public DatabaseHelper2(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE coordinate (field1 CHAR NOT NULL ,field2 INTEGER NOT NULL ,field3 INTEGER NOT NULL);");
db.execSQL("INSERT INTO spotit VALUES( test ,10,10);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
Then initiate the class in your main activity.
DatabaseHelper db = new DatabaseHelper(this);
db.getReadableDatabase();
db.close();
onCreate will be invoked when you call db.getReadableDatabase()
all you need is here:
http://developer.android.com/guide/topics/data/data-storage.html#db