following is the code that i used to alter my table but my app is crashing.
I have create this dbupdateadapter class separately. the records are being added, but the column is not added to the same table
public class dbupdateadapter {
public static final String TABLE_MEMBERID="memberid";
private static final String DATABASE_NAME="myfemmefab.db";
private static final String TABLE_NAME="registeration";
private static final int DATABASE_VERSION=2;
private static final String DATABASE_UPDATE="ALTER TABLE registeration ADD COLUMN(memberid TEXT);";
private static Context context1;
private dbhelper1 helper1;
private SQLiteDatabase sdb1;
public dbupdateadapter(Context con){
this.context1=con;
helper1=new dbhelper1(context1);
}
public class dbhelper1 extends SQLiteOpenHelper{
public dbhelper1(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase sdb1){
sdb1.execSQL(DATABASE_UPDATE);
}
#Override
public void onUpgrade(SQLiteDatabase sdb1,int oldver,int newver){
//android.util.Log.w("constants", "upgrading database will destroy the old data");
if(newver>oldver){
sdb1.execSQL(DATABASE_UPDATE);
}
//onCreate(sdb1);
}
}
public dbupdateadapter open() throws SQLException{
sdb1=helper1.getWritableDatabase();
return this;
}
void insertcoldata (String memberid){
ContentValues cv1=new ContentValues();
cv1.put(TABLE_MEMBERID, memberid);
sdb1.insert(TABLE_NAME, null, cv1);
}
public void close(){
helper1.close();
}
}
You should know that in
public void onCreate(SQLiteDatabase db ){ ... }
you should create your tables and populate them with some initial values and you didn't create table in your snippet of code.
for upgrating your tables you should use
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { ... }
Exactly from docs:
Called when the database needs to be upgraded. The implementation
should use this method to drop tables, add tables, or do anything else
it needs to upgrade to the new schema version.
Also have look at
SQL As Understood By
SQLite
Example of how to implement ALTER
TABLE
Use "ALTER TABLE registeration ADD memberid TEXT;"; or "ALTER TABLE registeration ADD COLUMN memberid TEXT;"; You have an error in syntax.
Where are you creating yout table? You are executing DATABASE_UPDATE in your onCreate().
Related
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.
I need only select operation. Therefore the data should be inserted before the application starts. What is the best way to do this?
Use an subclass of SQLiteOpenHelper to access your db. The onCreate will be called once, when the database is created.
To use your db you can then do:
DBHelper db = new DBHelper(context);
db.getReadableDatabase().query(....);
db.close();
To extend SQLiteOpenHelper do something like (the sql for the table creation is not tested!):
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "database.db";
private static final Integer DATABASE_VERSION = 1;
DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/*
* Called ONCE on the very first db access (when the db file is created)
*/
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE mytable (_id PRIMARY KEY AUTOINCREMENT, value TEXT); \n" +
"INSERT INTO mytable (value) VALUES ('data'));");
}
/*
* Called every time you open a database
*/
#Override
public void onOpen(SQLiteDatabase db) {
}
/*
* Called ONCE if DATABASE_VERSION has changed since the last instantiation of DBHelper
*/
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Hi I am new to android and I want to know how I can add buttons to my app programmatically . Actually the scenario is like this: In my app, I am having several default categories(given as buttons) to save data. I must provide an option add categories. When I click on the add categories button I must get an option to specify the category name and the sub category name(which i can do). But the thing is that I must get a new button with the entered name and it should not get deleted when i leave the application. Any help is highly appreciated.
To create database: refer How do I create a database in android?
Then you need to use DatabaseHelper class to insert,update or delete data on your database.
refer http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
Now you must be able to manage your data using DatabaseHelper class's object in your activity.
1.DatabaseCreator.java
public class DatabaseCreator extends SQLiteOpenHelper{
private static final String DB_NAME="database_name";
private static final int DB_VER=1;
private static final String TABLE_NAME="create table table_name(_id integer primary key autoincrement,field1 text not null,field2 text not null)";
public DatabaseCreator(Context context)
{
super(context,DB_NAME, null, DB_VER);
}
#Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL(TABLE_NAME);
}
#Override
public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
database.execSQL("DROP TABLE IF EXISTS table_name");
onCreate(database);
}
}
2.DatabaseHelper.java
public class DatabaseHelper
{
public static final String TABLENAME_DB="table_name";
public static final String KEY_ID="_id";
public static final String KEY_FIELD1="field1";
public static final String KEY_FIELD2="field2";
Context context;
SQLiteDatabase sqdb;
DatabaseCreator dbcreator;
public DatabaseHelper(Context context)
{
this.context=context;
}
public DatabaseHelper open() throws SQLException
{
dbcreator=new DatabaseCreator(context);
sqdb=dbcreator.getWritableDatabase();
return this;
}
public void close()
{
dbcreator.close();
}
public long addItem(String field1,String field2)
{
ContentValues values=new ContentValues();
values.put(KEY_FIELD1,field1);
values.put(KEY_FIELD2,field2);
return sqdb.insert(TABLENAME_DB,null,values);
}
public long deletItem(long _id)
{
return sqdb.delete(TABLENAME_DB, "_id="+_id,null);
}
}
Use this in your activity like:
DatabaseHelper db_helper=new DatabaseHelper(getApplicationContext());
db_helper.open();
db_helper.addItem("field1_value","field2_value");
db_helper.close();
I have a database that I have created with the following handler:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "trade.db";
public static final String RESOURCEID = "resourceid";
public static final String STARTPRICE = "startprice";
public static final String CURRENTBID = "currentbid";
public static final String EXPIRES = "expires";
public static final String BUYNOWPRICE = "buynowprice";
public static final String TYPE = "type";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE cards (_id INTEGER PRIMARY KEY AUTOINCREMENT, resourceid TEXT, startprice TEXT,currentbid TEXT, expires TEXT, buynowprice TEXT, type TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("cards",
"Upgrading database, which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS ");
onCreate(db);
}
}
I would like to display the items of this DB in a listview where items are selectable, unfortunately I am a complete android nooby (first application). Could someone please explain to me how I would do this? I am well aware there are some examples out there, but I am simply unable to follow them. Thanks
No tutorial, just some things you'll need:
The
getReadableDatabase()-method of
your SQLiteOpenHelper
A CursorAdapter
I can give you full example of how to show it... But it would make you a lazy... Whatever I can guide you... Like you have to create a layout for ListView and the layout for the items of ListView... You can do this also without creating these layouts... But whatever that is you need to create an adapter it could be SimpleAdapter... You have to put that adapter's record by HashMap... Like the example below:
SimpleAdapter adapter = new SimpleAdapter(this,appntmntHistoryList,R.layout.appointment_history_text,new String[] {"date","appntmnt_id"},new int[] {R.id.appDateHist, R.id.appWithFakeHist});
appointmentHistory.setAdapter(adapter);
This process is for record fetching from database store those records in key/value pair by HashMap...
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