A TOWN CODERS Forum Index A TOWN CODERS
something
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 




Databases--Discussion and Learning how the shit it works.

 
Post new topic   Reply to topic    A TOWN CODERS Forum Index -> Random
View previous topic :: View next topic  
Author Message
joulesqrd



Joined: 16 Jun 2011
Posts: 7
Location: United States

PostPosted: Thu Jun 16, 2011 8:53 am    Post subject: Databases--Discussion and Learning how the shit it works. Reply with quote

Here's the code (for derek's reference):

package com.atown.reviews;

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

public class Database {

private static final String DATABASE_NAME = "reviewsData";

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;


private class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context){ //extends from original name
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE reviews (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"title TEXT, value TEXT);");
ContentValues cv = new ContentValues();

/*cv.put("title", "Gravity, DeathStar I");
cv.put("value", "3.5303614E-7");
db.insert("reviews", null, cv);*/
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Reviews", "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS reviews");
onCreate(db);
}
}

private final Context mCtx;

public Database(Context c) {
mCtx = c;
}
public Database open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}

public void close() {
mDbHelper.close();
}

public String getAllReviews(String table)
{
open();
String r = "";
Cursor result = mDb.rawQuery("SELECT title, value FROM " + table, null);
result.moveToFirst();
while (!result.isAfterLast())
{
r += result.getString(0);
r += "\n---------------------------\n";
r += result.getString(1);
r += "\n\n";
result.moveToNext();
}
result.close();
close();
return r;
}
public String getAllReviews() { return getAllReviews("reviews"); }

public void addReview(String table, String title, String text)
{
open();
ContentValues cv = new ContentValues();
cv.put("title", title);
cv.put("value", text);
mDb.insert(table, null, cv);
close();
}
public void addReview(String title, String text) { addReview("reviews", title, text); }
}


And here's the code inside write:

input=(EditText)findViewById(R.id.log_entry);
title=(EditText)findViewById(R.id.write_title);

Button submitButton = (Button)findViewById(R.id.submit_log);
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("Title", title.getText().toString());
Log.v("EditText", input.getText().toString());
(new Database(v.getContext())).addReview(title.getText().toString(), input.getText().toString());

input.setText(null);
title.setText(null);
}
});

and read:
Database mDbHelper = new Database(this);
((TextView)findViewById(R.id.readView)).setText(mDbHelper.getAllReviews());



I'm going to try to use cursors to iterate through the database, but I don't know how far I can go with that. Might need to do this on friday.
Back to top
View user's profile Send private message AIM Address
RehdBlob



Joined: 14 Jun 2011
Posts: 2

PostPosted: Thu Jun 16, 2011 8:42 pm    Post subject: Reply with quote

I'll be playing around with this stuff today / tomorrow and / or attempting to figure out other functionalities of the Android platform
Back to top
View user's profile Send private message
Free Forum






PostPosted:      Post subject: ForumsLand.com

Back to top
Display posts from previous:   
Post new topic   Reply to topic    A TOWN CODERS Forum Index -> Random All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosted by ForumsLand.com - 100% free forum. Powered by phpBB 2.