I am exctracting data from sqlite database and i need to transfer a column exctracted from select statment to other activity on a textview. How do i do it. Here's my code.
db=database.getReadableDatabase();
Cursor c = db.rawQuery("SELECT lusername, lpwd, lname FROM login WHERE
lusername=userfield.getText().toString(),lpwd=pwdfield.getText().toString()",null);
if(c!=null)
{
Intent intent = new Intent(this, HomePage.class);
intent.putExtra("TITLEBAR", lname);
}
i want lname(column) to go to next activity's textview.
First of all I doubt on your select query whether its worked or not..
But you told "I am extracting data from sqlite database" fine,
then just make a String[] of lname column's values from your database cursor then pass that String[] to other activity using intent like,..
Create intent:
Intent intent_name = new Intent(this, intent_name.class);
intent_name.putStringArray("key",string_array);
Then in your intent_name.class do:
String[] array = getIntent().getStringArray("key");
Related
I am successfully able to add data to sqlite database,
now when i click on a listview item,
i want to get data from sqlite table ID which is same as listview position
:
My Database looks like this
You need to separately query for getting raw at particular position clicked in LisView.
int id = 0; // id of clicked item
StringBuilder query = new StringBuilder("SELECT * FROM table_name WHERE id = ").append(id);
Cursor cursor = db.rawQuery(query.toString(), null);
if (cursor.moveToFirst()) {
Intent intent = new Intent(context, IntentClass.class);
intent.putExtra("id",id);
intent.putExtra("link",cursor.getString(4));
intent.putExtra("lid",cursor.getString(5));
intent.putExtra("lpass",cursor.getString(6));
intent.putExtra("laddInfo",cursor.getString(7));
context.startActivity(intent);
}
cursor.close();
I have to save some data inside of SQLLite. This text data can contain a quote ('). Is there a way to escape this char on insert, and get it back when getting the data from the database?
In particular, the name is a references to a file. So the file can be named like "hel'lo.file". Before escaping it to the database, it should be "hel''lo.file". But when i get it back i need again "hel'lo.file" to be certain that the string inside the db matches the file name.
I'm using a content provider and a SQLiteOpenHelper.
Inside my content provider, for the insert i'm doing this:
_id = db.insert(TextEditorContract.NoteEntry.TABLE_NAME, null, values);
My insert inside my activity:
ContentValues values = new ContentValues();
...
values.put(NoteEntry.COLUMN_TITLE, getFileTitle());
Uri recordUri = contentResolver.insert(NoteEntry.CONTENT_URI, values);
Use SQLiteOpenHelper - then you can use prepared statements. See this question: Android SQLite Example
EDIT
String file = "/storage/emulated/0/Note/hello 'world.txt"
String sql = "SELECT _id FROM Recents WHERE percorso=? ORDER BY _id ASC";
String[] data = {file};
Cursor cursor = database.rawQuery(sql, data);
I use the following intent to pick an email-contact from the contacts app.:
Intent selectContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
selectContact.setType(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);
startActivityForResult(selectContact, RESULT_PICK_CONTACT);
On activity result i try to query the returned id like this:
Uri contactData = intent.getData();
String contactId = contactData.getLastPathSegment();
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID.concat(" = ?"),
new String[]{
contactId
},
null);
Unfortunally the Cursor i use to query the picked contact is always empty although the contact id returned seems to be valid.
What am i doing wrong?
Querying onto a different id solved the problem.
When picking Email contacts the CONTACT_ID constant doesnt seem to be the right identifier.
I changed Selection argument to use _ID instead:
ContactsContract.CommonDataKinds.Email._ID.concat(" = ?")
I've been thinking of ways to make this work, and I just dont get it.
I have several HTML files that are viewed using webView, I want to create a bookmark button on this WebView page.
If I create a DB with a table and three columns - containing the _id, the Title, and the URL, and populate it with the Titles and the URLs(from assets folder) and give each an IDs. When the "Add to Bookmark" button is clicked in the WebView activity, can it populate a ListView in the Favorites activity, and when the title is clicked, it locates the URL that corresponds in the SQLDB and then open it.
I have googled on SQLDB, but most of the tutorials do not make use of an already stored data in DB.
Thank you.
Alright use this code :
// Saving the bookmark via ContentProvider
final ContentValues bookmarkValues = new ContentValues();
bookmarkValues.put(Browser.BookmarkColumns.TITLE, title);
bookmarkValues.put(Browser.BookmarkColumns.URL, url);
bookmarkValues.put(Browser.BookmarkColumns.BOOKMARK, 1);
final Uri newBookmark = getContentResolver().insert(Browser.BOOKMARKS_URI, bookmarkValues);
// Retrieving
Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI,
null, null, null, Browser.BookmarkColumns.CREATED);
cursor.moveToFirst();
final String stitle = cursor.getString(cursor.getColumnIndex("title"));
final String surl = cursor.getString(cursor.getColumnIndex("url"));
What i'm trying to do: ListView activity A, I touch an item, it opens a new listview activity. I take the primary key from the database row selected in list A and use putExtra. In activity B, in onCreate I want to check and see if there is any row in Table B where the column TOPIC_CONTENT_TOPIC_ID has a value that equals the primary key from table A that was putExtra. If the table is empty, or if there is no matching ID, i want to create a new row where column TOPIC_CONTENT_TOPIC_ID now equals the primary key from table A. Here is my code. It crashes on the db.query();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.topic_content);
dbMaker = new DatabaseMaker(this);
SQLiteDatabase db = dbMaker.getReadableDatabase();
Intent myIntent = getIntent();
int topicIDInt = (int) myIntent.getLongExtra("com.spentakapps.ScripturalConcordance.Topics", -1);
String topicID = Long.toString(myIntent.getLongExtra("com.spentakapps.ScripturalConcordance.Topics", -1));
String WHERE = DatabaseStructure.TOPICS_CONTENT_TOPIC_ID + " = " + topicID;
Cursor cur = db.query(DatabaseStructure.TABLE_TOPICS_CONTENT, new String[] {DatabaseStructure.TOPICS_CONTENT_CONTENT},WHERE, null, null, null, null);
if (cur.getCount() <= 0)
{
ContentValues values = new ContentValues();
values.put(DatabaseStructure.TOPICS_CONTENT_TOPIC_ID, topicIDInt);
db = dbMaker.getWritableDatabase();
db.insert(DatabaseStructure.TABLE_TOPICS_CONTENT, null, values);
db = dbMaker.getReadableDatabase();
cur = db.query(DatabaseStructure.TABLE_TOPICS_CONTENT, new String[] {DatabaseStructure.TOPICS_CONTENT_TOPIC_ID},WHERE, null, null, null, null);
}
View addContentButton = findViewById(R.id.content_button_add);
addContentButton.setOnClickListener(this);
//Set up data binding
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.topiccontentlineitem,cur, FROM,TO);
setListAdapter(adapter);
}
My recommendation for these sorts of issues is always to log into the emulator using:
adb shell
then open the sqlite db directly
# cd /data/data/<your application package>/databases/
# ls
# sqlite3 <your database file>
once inside you should be able to try to manually run your sql to make sure it works:
select id from TABLE_TOPICS_CONTENT where id='someidvalue' limit 1
When you say it crashes on db.query, there are two db.query statemenets in your above code. My guess is that you are running into significant problems since you are calling dbMaker.getReadableDatabase(); several times. Try to just call it once in the beginning as runnable and try to verify that your sql is valid using those. If that fails, attach your error log.