bit of a beginner in android and so far i have built a small task to send some information to a database.
In the main activity it gets the values and then tries to pass it along to the database using the bellow methords.
(in extends actvity)
private DBManager dbManager;
(on create)
dbManager = new DBManager(this);
dbManager.open();
(in on create after values are laid out)
dbManager.insert( id, cat, title, datec, datem, name, image, video);
it then passes the data along to the DBManager class
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 DBManager {
private DatabaseHelper dbHelper;
private Context context;
private SQLiteDatabase database;
public DBManager(Context c) {
context = c;
}
public DBManager open() throws SQLException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public void insert(String id, String cat, String title, String datec, String datem, String name, String image, String video) {
ContentValues contentValue = new ContentValues();
contentValue.put(DatabaseHelper.TAG_TUTID, id);
contentValue.put(DatabaseHelper.TAG_CATID, cat);
contentValue.put(DatabaseHelper.TAG_TUTTITLE, title);
contentValue.put(DatabaseHelper.TAG_NAME, name);
contentValue.put(DatabaseHelper.TAG_DATEC, datec);
contentValue.put(DatabaseHelper.TAG_DATEM, datem);
contentValue.put(DatabaseHelper.TAG_IMAGE, image);
contentValue.put(DatabaseHelper.TAG_VIDEO, video);
database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
Log.d("testing insert: ", cat);
}
public Cursor fetch() {
String[] columns = new String[] { DatabaseHelper._ID, DatabaseHelper.TAG_TUTID, DatabaseHelper.TAG_CATID, DatabaseHelper.TAG_TUTTITLE, DatabaseHelper.TAG_NAME, DatabaseHelper.TAG_DATEC, DatabaseHelper.TAG_DATEM, DatabaseHelper.TAG_IMAGE, DatabaseHelper.TAG_VIDEO };
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public int update(long _id, String id, String cat, String title, String datec, String datem, String name, String image, String video) {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.TAG_TUTID, id);
contentValues.put(DatabaseHelper.TAG_CATID, cat);
contentValues.put(DatabaseHelper.TAG_TUTTITLE, title);
contentValues.put(DatabaseHelper.TAG_NAME, name);
contentValues.put(DatabaseHelper.TAG_DATEC, datec);
contentValues.put(DatabaseHelper.TAG_DATEM, datem);
contentValues.put(DatabaseHelper.TAG_IMAGE, image);
contentValues.put(DatabaseHelper.TAG_VIDEO, video);
Log.d("testing update: ", image);
int i = database.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper._ID + " = " + _id, null);
return i;
}
public void delete(long _id) {
database.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper._ID + "=" + _id, null);
}
}
Which uses the bellow to create a database.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
// Table Name
public static final String TABLE_NAME = "TODOS";
// Table columns
public static final String _ID = "_id";
public static final String TAG_TUTID = "tutID";
public static final String TAG_CATID = "catID";
public static final String TAG_TUTTITLE = "tutTitle";
public static final String TAG_NAME = "tutText";
public static final String TAG_DATEC = "tutDateCreated";
public static final String TAG_DATEM = "tutDateModified";
public static final String TAG_IMAGE = "imageID";
public static final String TAG_VIDEO = "videoID";
// Database Information
static final String DB_NAME = "tuts.DB";
// database version
static final int DB_VERSION = 1;
// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TAG_TUTID + " TEXT NOT NULL, " + TAG_CATID + " TEXT, " + TAG_TUTTITLE+ " TEXT, " + TAG_NAME+ " TEXT, " + TAG_DATEC+ " TEXT, " + TAG_DATEM+ " TEXT, " + TAG_IMAGE+ " TEXT, " + TAG_VIDEO+ " TEXT);";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {db.execSQL(CREATE_TABLE);
Log.d("testing create: ", DB_NAME);}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
Log.d("testing delete: ", DB_NAME);
}
}
the log cat shows that the insert methrod is being run, but that is the only thing that logcats, the creation of the database itself doesnot, furthermore i cannot find a new database using device explorer.
Any advice on where my problem lies would be greatly appreciated. Thank you for your time.
Aucun commentaire:
Enregistrer un commentaire