Reputation: 145
I am new in android.I wanting store my audio data to a database. I can create Database and a table fore saving data.but problem is when I exit from app and run again my data have been stored before not exists at all. I need those data saved before.but I have this issue with my database.i am certain that data stored truly. May be problem it is that every time I run app,database overwrite on existing database.actually what is issue? Your help make me happy.thanks. Here is my classes which I used and truly worked.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySqilteOpenHelper extends SQLiteOpenHelper
{
public static final String TABLE_AUDIOS = "audios";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_fORMAT = "format";
public static final String COLUMN_PATH = "path";
public static final String COLUMN_TOTAL_TIME = "total_time";
public static final String COLUMN_SIZE = "size";
public static final String COLUMN_CREATEDATE = "create_date";
public static final String COLUMN_CREATETIME= "create_time";
private static final String DATABASE_NAME = "my_database.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table "
+ TABLE_AUDIOS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_NAME
+ " text not null, " + COLUMN_fORMAT + " text not null, " +COLUMN_PATH + " text not null, " + COLUMN_TOTAL_TIME + " text not null, " + COLUMN_SIZE + " text not null, " + COLUMN_CREATETIME + " text not null, "+COLUMN_CREATEDATE + " text not null);";
public MySqilteOpenHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("database create statement:", DATABASE_CREATE);
}
@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL( ""+DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
{
Log.w(MySqilteOpenHelper.class.getName(),
"Upgrading database from version " + oldVersion +
" to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_AUDIOS);
onCreate(database);
}
}
And this is a class which by that can insert and get rows.
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class Interface_For_UpgradingAudiosTable
{
private SQLiteDatabase database;
private MySqilteOpenHelper dbHelper;
private String[] allColumns = { MySqilteOpenHelper.COLUMN_ID,
MySqilteOpenHelper.COLUMN_NAME,
MySqilteOpenHelper.COLUMN_fORMAT,MySqilteOpenHelper.COLUMN_PATH ,MySqilteOpenHelper.COLUMN_TOTAL_TIME ,MySqilteOpenHelper.COLUMN_SIZE , MySqilteOpenHelper.COLUMN_CREATETIME , MySqilteOpenHelper.COLUMN_CREATEDATE };
private String[] mycolumns={MySqilteOpenHelper.COLUMN_NAME,
MySqilteOpenHelper.COLUMN_fORMAT,MySqilteOpenHelper.COLUMN_PATH ,MySqilteOpenHelper.COLUMN_TOTAL_TIME ,MySqilteOpenHelper.COLUMN_SIZE , MySqilteOpenHelper.COLUMN_CREATETIME , MySqilteOpenHelper.COLUMN_CREATEDATE };
public Interface_For_UpgradingAudiosTable(Context context)
{
dbHelper = new MySqilteOpenHelper(context);
}
public void open() throws SQLException
{
database = dbHelper.getWritableDatabase();
}
public void close()
{
dbHelper.close();
}
public StructureDetails createAudio(StructureDetails sd)
{
ContentValues values = new ContentValues();
values.put(MySqilteOpenHelper.COLUMN_NAME, sd.getName());
values.put(MySqilteOpenHelper.COLUMN_fORMAT, sd.getFormat());
values.put(MySqilteOpenHelper.COLUMN_PATH, sd.getPath());
values.put(MySqilteOpenHelper.COLUMN_TOTAL_TIME, sd.getTotalTime());
values.put(MySqilteOpenHelper.COLUMN_SIZE, sd.getSize());
values.put(MySqilteOpenHelper.COLUMN_CREATETIME, sd.getCreateTime());
values.put(MySqilteOpenHelper.COLUMN_CREATEDATE, sd.getCreateDate());
long insertId = database.insert(MySqilteOpenHelper.TABLE_AUDIOS,null,values);
Log.d("insert iddddddddddddddddddddddddddddddddd:",insertId+"");
Cursor cursor = database.query(MySqilteOpenHelper.TABLE_AUDIOS,
allColumns, MySqilteOpenHelper.COLUMN_ID + " = " +
insertId, null,
null, null, null);
cursor.moveToFirst();
StructureDetails newAudio = cursorToAudio(cursor);
cursor.close();
return newAudio;
}
/*public void deleteAudio(StructureDetails Audio)
{
String path = Audio.getPath();
System.out.println("Comment deleted with id: " + path);
database.delete(MySqilteOpenHelper.TABLE_AUDIOS,
MySqilteOpenHelper.COLUMN_PATH
+ " = " + path, null);
}*/
public ArrayList<StructureDetails> getAllAudiosFromDb()
{
List<StructureDetails> audiosList = new ArrayList<StructureDetails>();
Log.d("errorrrrrrrrrrrr in get allllll", "");
Cursor cursor = database.query(MySqilteOpenHelper.TABLE_AUDIOS,
allColumns, null, null, null, null, null);
if(!cursor.moveToFirst())
while (!cursor.isAfterLast())
{
StructureDetails audio = cursorToAudio(cursor);
audiosList.add(audio);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return (ArrayList<StructureDetails>) audiosList;
}
private StructureDetails cursorToAudio(Cursor cursor)
{
StructureDetails Audio = new StructureDetails();
Audio.setId(cursor.getLong(0));
Audio.setName(cursor.getString(1));
Audio.setFormat(cursor.getString(2));
Audio.setPath(cursor.getString(3));
Audio.setTotalTime(cursor.getString(4));
Audio.setSize(cursor.getString(5));
Audio.setCreateTime(cursor.getString(6));
Audio.setCreateDate(cursor.getString(7));
return Audio;
}
}
Upvotes: 1
Views: 1804
Reputation: 145
my database worked truely if I had this code insted above code:
enter code herepublic ArrayList<StructureDetails> getAllAudiosFromDb()
{
List<StructureDetails> audiosList = new ArrayList<StructureDetails>();
Log.d("errorrrrrrrrrrrr in get allllll", "");
Cursor cursor = database.query(MySqilteOpenHelper.TABLE_AUDIOS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();// this is changed!
while (!cursor.isAfterLast())
{
StructureDetails audio = cursorToAudio(cursor);
audiosList.add(audio);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return (ArrayList<StructureDetails>) audiosList;
}
And above answer by "Arju" may be helpful.Neve eless I think I had problem on my method "getAllAudiosFromDb".may be this helped u.
Upvotes: 0
Reputation: 10083
Change create table
to CREATE TABLE IF NOT EXISTS
Otherwise your table will be create every time and data will be lost
So it must be
private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ TABLE_AUDIOS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_NAME
+ " text not null, " + COLUMN_fORMAT + " text not null, " +COLUMN_PATH + " text not null, " + COLUMN_TOTAL_TIME + " text not null, " + COLUMN_SIZE + " text not null, " + COLUMN_CREATETIME + " text not null, "+COLUMN_CREATEDATE + " text not null);";
UPDATE
onCreate()
and onUpgrade()
methods are really called the first time when Db is created. In facts, it's checked in getReadableDatabase()
or getWritebleDatabase()
methods of SQLiteOpenHelper. It will check if the DB already exist on the data directory and what is it's version. According to this it will either call onCreate()
, or onUpgrade()
Or nothing, if db file exist in correct version.
You can search your code for executing myDBHelper.getReadable(Writable)Database()
. That is the time when this check will be performed.
Upvotes: 1