开发者

Sqlite create table only in first launch of application

I make application with sqlite3. I want to create tables of the ap开发者_如何学运维p only first time user start application. How can I do that?


You would need to do this in the AppDelegate under

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

You would probably check if the database exists first:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *databasePath = [documentsPath stringByAppendingPathComponent:@"Database.db"];

BOOL dbExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

if (dbExists == NO) {
   //database doesn't exist
   //create your database here
    sqlite3* db;
    int error;
    if((error = sqlite3_open([databasePath UTF8String], &db)) != SQLITE_OK) {
       NSLog(@"sqlite3_open failed(%d) with %@", error, [databasePath UTF8String]);
       sqlite3_close(db);
    }
}

See the following question as to why this works: iPhone create SQLite database at runtime?

Here is another tutorial: http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/


i think this application code will help you.

package x.y;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button buttonX = (Button) findViewById(R.id.btn);
        buttonX.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                SQLiteDatabase db = openOrCreateDatabase("fd",
                        SQLiteDatabase.CREATE_IF_NECESSARY, null);

                db.execSQL("CREATE TABLE tbl1(col1 TEXT, col2 INTEGER)");

                ContentValues values = new ContentValues();

                values.put("col1", "Shaktimaan");
                values.put("col2", 420);

                db.insert("tbl1", null, values);

                db.close();
            }
        });
    }
}


You can to create table in Sqlite3 and save extension sqlite. And to save database in one drive, after use database in Apps that import in your apps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜