【问题标题】:Sqlite create table only in first launch of applicationSqlite 仅在首次启动应用程序时创建表
【发布时间】:2013-05-28 00:00:29
【问题描述】:

我使用 sqlite3 制作应用程序。我只想在用户第一次启动应用程序时创建应用程序表。我该怎么做?

【问题讨论】:

    标签: ios ios4


    【解决方案1】:

    您需要在 AppDelegate 下执行此操作

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

    你可能会先检查数据库是否存在:

    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);
        }
    }
    

    请参阅以下问题,了解为什么会这样:iPhone create SQLite database at runtime?

    这是另一个教程: http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/

    【讨论】:

      【解决方案2】:

      我认为这个应用程序代码会对你有所帮助。

      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();
                  }
              });
          }
      }
      

      【讨论】:

        【解决方案3】:

        您可以在Sqlite3 中创建表并保存扩展sqlite。并将数据库保存在一个驱动器中,在您的应用程序中导入的应用程序中使用数据库后。

        【讨论】:

          猜你喜欢
          • 2011-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多