final SQLiteDatabase mydb = openOrCreateDatabase("phone_calls",MODE_PRIVATE,null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS Numbers(PhoneNumber VARCHAR,FullName VARCHAR,Cost VARCHAR );");
mydb.execSQL("CREATE TABLE IF NOT EXISTS Calls(FromNum VARCHAR,ToNum VARCHAR,duration VARCHAR );");
Ive created this sqlite in Activity 1. i inserted some data to the first table in first activity.
now i created a second activity and i want to insert data to the other table?what shall i do? create the database again in the second activity and insert in it? or is there any other way?
First of all, you should read the docs - https://developer.android.com/training/basics/data-storage/databases.html
Next, you should try to write simple application in order to use new knowledge.
If it fails, then post the question with more code and Logcat output.
Just create a database helper class which will create all your tables,
then create needed methods like saveObject(YourObject), deleteObject(id),
etc...
Make your helper singleton and initiate it once in Application in onCreate()
and just use it in your activities.
For example you can look here: https://gist.github.com/mikeplate/9173040
Related
I have been using UCanAccess to use Access databases my problem is when i want to delete a recor this returns automatically.
For example if i have:
Table Names
Id Name
1 Jessy
2 Abraham
String deleteQuery = "DELETE From Names where Id =?";
PreparedStatement pstm = con.getConnection().prepareStatement(deleteQuery);
pstm.setInt(1, 1); // "id" is type numeric
pstm.executeUpdate();
pstm.close();
it will works And then if i open the database the recor will be there!
that's my problem. (i hide the connection code but i have it)
Try to use compact feature provided by Access. On the Tools menu, point to Database Utilities, and then click Compact and Repair Database. This might help.
Do you do the commit after? If not and autocommit=false, just do it.
I found the problem, I was using data type OLE to save images simple sentences doesn't works so the way to delete a row with OLE field is creating Database and Table objects from java. It works.
I have a table of client contain the client id, name and last name, job ..etc...
also I have 16 tables of the grants of client each table contain a different data(different grant) relate with the client by a foreign key of "id".......
for example this is costumer table"clients":
enter image description here
and this is an example for an grant of client called order:
enter image description here
So when I search for client, I search also for all the grants that he benefited them, and maybe a client can benefit from the same grant more than once........that's means we can find more than one row in the same table relate with the same client:
As you see in the table Of order we have two row relate with the client who have id=2.: So I want to make one query to select all data from the first table and the second of the client "Where id =2"
the problem is how I can get all the information relate with that client and to which row from which table ??
If you want to access data from particular Table you can see the following code It may help you
public Cursor getInformation(databaseOperations dbo){
SQLiteDatabase Db = dbo.getReadableDatabase();
String [] columns = {TableData.TableInfo.USER_NAME, TableData.TableInfo.CONTACT_NO, TableData.TableInfo.EMAIL_ID,TableData.TableInfo.PROFILE_PATH};
Cursor CR =Db.query(TableData.TableInfo.TABLE_NAME,columns,null,null,null,null,null);
return CR;
}
If you need data from particular row or column then you can pass different argument instead of null in DB.query.
To know about different parameters you can see just this answer click here
I hope it will help you. To know more about parameter details of db.query() function you can go through google search.
I need to have a read only database for an android application with three simple tables:
Countries
Cities ( country_id foreign key)
PhoneCountryCodes (country_id foreign key)
I have .csv files that I need to extract data from and fill in these tables. The purpose for these tables is for the android app reading purposes and data validation.
The link here shows how to add data to the managed database Sqlite for android. Yet it seems from the following code that I need a Context to instantiate the DbHelper class:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
In other words, I want to add data to the database once and for all (basically Country data, cities and phone country codes), outside of the Activity context.
use getApplicationContext(); If you don't want activity context.
I am populating an alertDialog from my database allowing a user to select a preset email message template. The populating seems to work but I cannot delete or edit it, only add to it. I wanted to delete all the rows and basically repopulate it from the beginning but I can seem to delete a single entry or all entries.
This is my deleting method in my DB Handler class:
public void deleteAppointment() {
ourDatabase.delete(DATABASE_TABLETEMP, null, null);
}
This code I have used to try and delete all the rows but it dosent. Can anyone see where I am going wrong?
You can do:
Drop table tablename
--run rebuild scripts
I am attempting to create a test database (based off of my production db) at runtime, but rather than have to maintain an exact duplicate test db i'd like to copy the entire data structure of my production db at runtime and then when I close the test database, drop the entire database.
I assume I will be using statements such as:
CREATE DATABASE test //to create the test db
CREATE TABLE test.sampleTable LIKE production.sampleTable //to create each table
And when I am finished with the test db, calling a close method will run something like:
DROP DATABASE test //delete the database and all its tables
But how do I go about automatically finding all the tables within the production database without having to manually write them out. The idea is that I can manipulate my production db without having to be concerned with maintaining the structure identically within the test db.
Would a stored procedure be necessary in this case? Some sample code on how to achieve something like this would be appreciated.
If the database driver you are using supports it, you can use DatabaseMetaData#getTables to get the list of tables for a schema. You can get access to DatabaseMetaData from Connection#getMetaData.
In your scripting language, you call "SHOW TABLES" on the database you want to copy. Reading that result set a row at a time, your program puts the name of the table into a variable (let's call it $tablename) and can generate the sql: "CREATE TABLE test.$tablename LIKE production.$tablename". Iterate through the result set and you're done.
(You won't get foreign key constraints that way, but maybe you don't need those. If you do, you can run "SHOW CREATE TABLE $tablename" and parse the results to pick out the constraints.)
I don't have a code snippet for java, but here is one for perl that you could treat as pseudo-code:
$ref = $dbh->selectall_arrayref("SHOW TABLES");
unless(defined ($ref)){
print "Nothing found\n";
} else {
foreach my $row_ref (#{$ref}){
push(#tables, $row_ref->[0]);
}
}
The foreach statement iterates over the result set in an array reference returned by the database interface library. The push statement puts the first element of the current row of the result set into an array variable #tables. You'd be using the database library appropriate for your language of choice.
I would use mysqldump : http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
It will produce a file containing all the sql commands needed to replicate the prod database
The solutions was as follows:
private static final String SQL_CREATE_TEST_DB = "CREATE DATABASE test";
private static final String SQL_PROD_TABLES = "SHOW TABLES IN production";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute(SQL_CREATE_TEST_DB);
SqlRowSet result = jdbcTemplate.queryForRowSet(SQL_PROD_TABLES);
while(result.next()) {
String tableName = result.getString(result.getMetaData().getColumnName(1)); //Retrieves table name from column 1
jdbcTemplate.execute("CREATE TABLE test2." + tableName + " LIKE production." + tableName); //Create new table in test2 based on production structure
}
This is using Spring to simplify the database connection etc, but the real magic is in the SQL statements. As mentioned by D Mac, this will not copy foreign key constraints, but that can be achieved by running another SQL statement and parsing the results.