Creating Database in Blackberry phone - database

please help me out from this
I have created a database in Blackberry phone i have also created a table inside the database but on clicking on the database on blackberry simulator it shows UNABLE TO DISPLAY file ,and the code I have written is
class CreateDatabaseSchemaScreen extends MainScreen{
Database d;
public CreateDatabaseSchemaScreen(){
try
{
URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" + "MyEncryptedDatabase.db");
DatabaseSecurityOptions dbso = new DatabaseSecurityOptions(true);
d = DatabaseFactory.create(myURI,dbso);
d= DatabaseFactory.open(myURI);
Statement s= d.createStatement("CREATE TABLE 'People' ( " +
"'Name' TEXT, " +
"'Age' INTEGER )" );
s.prepare();
s.execute();
s.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
}
}

Database files can't be opened directly, you'll need a third party desktop tool that allows management of it. You can find a list of these tools here (generally they're free):
http://www.sqlite.org/cvstrac/wiki?p=ManagementTools
In your BB simulator settings, you will see an option that specifies where the SDCard data is located. Once you choose a management tool, open the database in that location, and you'll be able to see if the table has been created successfully.

Related

Xamarin Forms - How do i use a Premade Local Database? [Solved] [duplicate]

I have started using the Xamarin plugin for Visual Studio to create an Android app.
I have a local SQL database, and I want to call it to display data. I don't see how I can do this. Is it possible?
After thinking this was a trivial thing to do, I was proven wrong when I tried setup a quick test project. This post will contain a full tutorial on setting up a DB for an Android App in Xamarin that will come in handy as a reference for future Xamarin users.
At a glance:
Add Sqlite.cs to your project.
Add your database file as an Asset.
Set your database file to build as an AndroidAsset.
Manually copy the database file out of your apk to another directory.
Open a database connetion using Sqlite.SqliteConnection.
Operate on the database using Sqlite.
Setting up a local database for a Xamarin Android project
1. Add Sqlite.cs to your project.
Start by going to this repository and downloading Sqlite.cs; this provides the Sqlite API that you can use to run queries against your db. Add the file to your project as a source file.
2. Add DB as asset.
Next, get your DB and copy it into the Assets directory of your Android project and then import it into your project so that it appears beneath the Assets folder within your solution:
I'm using the Chinook_Sqlite.sqlite database sample renamed to db.sqlite from this site throughout this example.
3. Set DB to build as AndroidAsset.
Right click on the DB file and set it to build action AndroidAsset. This will ensure that it is included into the assets directory of the APK.
4. Manually copy DB out of your APK.
As the DB is included as an Asset (packaged within the APK) you will need to extract it out.
You can do this with the following code:
string dbName = "db.sqlite";
string dbPath = Path.Combine (Android.OS.Environment.ExternalStorageDirectory.ToString (), dbName);
// Check if your DB has already been extracted.
if (!File.Exists(dbPath))
{
using (BinaryReader br = new BinaryReader(Android.App.Application.Context.Assets.Open(dbName)))
{
using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int len = 0;
while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write (buffer, 0, len);
}
}
}
}
This extracts the DB as a binary file from the APK and places it into the system external storage path. Realistically the DB can go wherever you want, I've just chosen to stick it here.
I also read that Android has a databases folder that will store databases directly; I couldn't get it to work so I've just ran with this method of using an existing DB.
5. Open DB Connection.
Now open a connection to the DB through the Sqlite.SqliteConnection class:
using (var conn = new SQLite.SQLiteConnection(dbPath))
{
// Do stuff here...
}
6. Operate on DB.
Lastly, as Sqlite.net is an ORM, you can operate on the database using your own data types:
public class Album
{
[PrimaryKey, AutoIncrement]
public int AlbumId { get; set; }
public string Title { get; set; }
public int ArtistId { get; set; }
}
// Other code...
using (var conn = new SQLite.SQLiteConnection(dbPath))
{
var cmd = new SQLite.SQLiteCommand (conn);
cmd.CommandText = "select * from Album";
var r = cmd.ExecuteQuery<Album> ();
Console.Write (r);
}
Summary
And that's how to add an existing Sqlite database to your Xamarin solution for Android! For more information check out the examples included with the Sqlite.net library, its unit tests and the examples in the Xamarin documentation.
Here is the one that I'm using and it's working
install the Sqlite plugin
create interface to access different platforms services
create a model for the table
implement the interface that you created earlier on all of the
platform you want to use
use the plugin to create, get, insert, etc on your table
for more detailed information check this

How to give option to user for choosing the file folder in xamarin forms?

I would like to give user an ability to chose the desired file location in his/her device to save information.
I didn't find anything on the Internet that would work for all the platforms. The only options seem to be available for Android.
You can use the package Xamarin.Plugin.FilePicker from NuGet. It is a FilePicker Plugin for Xamarin.Forms.
Example:
try
{
FileData fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
return; // user canceled file picking
string fileName = fileData.FileName;
string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);
System.Console.WriteLine("File name chosen: " + fileName);
System.Console.WriteLine("File data: " + contents);
}
catch (Exception ex)
{
System.Console.WriteLine("Exception choosing file: " + ex.ToString());
}
For iOS ,you need to Configure iCloud Driver for your app.For more detail and if you want download a sample you can refer here.
For IOS you can read this document.
https://possiblemobile.com/2013/04/using-xcode-to-test-location-services/
All,( android and IOS ) can be manipulated by dependency injection. So you are using native code of android and of IOS and choose/control in xamarin forms.
Good luck
Guilherme

Create persistent Sqlite db in Windows phone 8

I am trying my hands on Windows phone 8 applications and I am stuck into a weird situation here. I am using sqlite in order to create sqlite db and add values into the database. I am able to create the database and add the values in the database successfully but I am having a weird situation here.
Everytime I close the emulator and start the project again the database gets created again which should not be happening because I created the db the very first time I run the application.
Does anybody know why, and how I can prevent it from recreating the database each time?
public string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "aa.sqlite"));
private SQLiteConnection dtCon;
public MainPage()
{
InitializeComponent();
CreateDatabase();
dtCon = new SQLiteConnection(DB_PATH);
var tp = dtCon.Query<Contacts>("select * from contacts").ToList();
}
private async void CreateDatabase()
{
bool isDatabaseExisting = false;
//Checking if database already exists
try
{
Windows.Storage.StorageFile storagefile = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("aa.sqlite");
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
//if not exists then creating database
if (!isDatabaseExisting)
{
String str = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "sqlite.db");
AddDataToDB(DB_PATH);
}
}
private void AddDataToDB(string str)
{
// Create the database connection.
dtCon = new SQLiteConnection(str);
// Create the table Task, if it doesn't exist.
dtCon.CreateTable<Contacts>();
Contacts oContacts = new Contacts();
oContacts.Name = "dfgdf";
oContacts.Detail = "asdfsf";
dtCon.Insert(oContacts);
}
I'm pretty sure when you close your emulator and restart, you're basically just uninstalling the application. Which is why your files or not there anymore -- as it looks like you're storing your data in isolated storage. I do not know if there is anyway around this.
You can buy a very cheap Windows 8/8.1 Phone and the files will persist until you manually uninstall the test application.
As #Chubosaurus says, closing and re-opening the emulator will remove all the apps. You can generally keep it running as long as you want and keep re-deploying your app to the emulator (although obviously rebooting the host PC will kill it).
You can save and restore the data from your emulator image via the ISETool command. See more here
Try adding Console.WriteLine("True"); and Console.WriteLine("False"); into the expected places after checking isDatabaseExisting to see/understand what the code path really is.

Neo4j...how to get a visual representation of my data?

I have loaded an embedded instance of Neo4j with some data, and would like to know how I can now view this graph. I saw an intro video here: http://video.neo4j.org/m9FD/how-to-get-started-with-neo4j-119/. Here the guy opened an instance in his web browser via Heroku and was able not only see the data in the graph, but also enter new data and search for both nodes and relationships. How do I see the data I have entered into the graph-database?
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( "var/graphDb" );
registerShutdownHook(graphDb);
WrappingNeoServerBootstrapper srv = new WrappingNeoServerBootstrapper( graphDb );
srv.start();
for (Statement s : statements){
Transaction tx = graphDb.beginTx();
try{
firstNode = graphDb.createNode();
firstNode.setProperty("message", s.getFirstTerm());
secondNode = graphDb.createNode();
secondNode.setProperty( "message", s.getSecondTerm());
relation = firstNode.createRelationshipTo(secondNode, s.getRelationshipType());
//relation.setProperty("message", "crazy cruel");
tx.success();
}
finally{
tx.finish();
}
}
srv.stop();
}
you could either
fire up a server as part of your embedded instance, see http://docs.neo4j.org/chunked/snapshot/server-embedded.html
use Neoclipse and point it to your (shut down) database, see http://vimeo.com/12014944 aand https://github.com/neo4j/neoclipse
install Neo4j Server http://docs.neo4j.org/chunked/snapshot/server.html, point out your database and use the webadmin tool for visualization.
Would that work?

Can I download an SQLite db on /sdcard and access it from my Android app?

I already found out that there is no way to bundle files in an .apk and have them on /sdcard, the best option so far being to download the large files upon first run. I came accross a tutorial saying how to bundle an sqlite db with the apk and then copy it so that it can be accessed with SQLiteDatabase (thus doubling the space needed, and not using /sdcard at all).
http://developer.android.com/guide/topics/data/data-storage.html#db says all databases MUST be in /data/data/package_name/databases.
Is that really so? Is there a way to trick the framework into opening a database that is placed on the /sdcard partition? Is there a way to use another SQLite java wrapper/framework to access such databases?
If the answer to the above is 'No', what other options do I have? My data is very well represented in a relational model, but is just too big, plus, I want to be able to update it without the need to reinstall/upgrade the entire app.
Sure you can. The docs are a little conflicting about this as they also say that no limitations are imposed. I think they should say that relative paths are to the above location and dbs there ARE private. Here is the code you want:
File dbfile = new File("/sdcard/mydb.sqlite" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? " + db.isOpen());
Try this:
String dir = Environment.getExternalStorageDirectory().getPath()
File dbfile = new File(dir+"/mydb.sqlite");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? " + db.isOpen());
Just an idea:
you can put your database.db into the assets folder.
if your database.db file is larger than 2Mb the system is unable to compress it, so you need other one options
you can rename your database.db for example database.jit or database.mp3 - which are not compressed, than at the first run you can rename it to database.db
check this out ...
storing android application data on SD Card
I share next code. Declare opcionesMenu Vector<String>
Vector < String > opcionesMenu = new Vector< String >();
// the database is SDCard. I saw the code to Blackberry (net.rim)
String rutaDB = "/storage/extSdCard/Android/databases/Offshore.db";
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(rutaDB, null);
Cursor cursor = db.rawQuery("SELECT Codigo, Nombre FROM Ruta ORDER BY Codigo, Nombre", null);
if (cursor.moveToFirst())
{
do
{
opcionesMenu.add(cursor.getString(0) + " - " + cursor.getString(1));
} while(cursor.moveToNext());
}
db.close();

Resources