如何在xamarin.android中的一个数据库中创建两个表

时间:2015-07-16 07:10:30

标签: android sqlite xamarin

这段代码没有创建第二个表请帮帮我......它创建了第一个表。只发布第二个...... 我在xamarin.android工作 我认为它已经创建了一个带有一个表的数据库,所以我卸载了我的应用程序并尝试了仍然没有更改..         公共数据库()         {

    }

    public Database(string sqldb_name)
    {
        try
        {
            CreateDatabase(sqldb_name);
        }
        catch (SQLiteException ex) 
        {
            sqldb_message = ex.Message;
        }
    }
    //Gets or sets value depending on database availability
    public bool DatabaseAvailable
    {
        get{ return sqldb_available; }
        set{ sqldb_available = value; }
    }
    //Gets or sets the value for message handling
    public string Message
    {
        get{ return sqldb_message; }
        set{ sqldb_message = value; }
    }
    //Creates a new database which name is given by the parameter
    public void CreateDatabase(string sqldb_name)
    {
        try
        {
            sqldb_message = "";
            string sqldb_location = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            string sqldb_path = Path.Combine(sqldb_location, sqldb_name);
            bool sqldb_exists = File.Exists(sqldb_path);
            if(!sqldb_exists)
            {
                sqldb = SQLiteDatabase.OpenOrCreateDatabase(sqldb_path,null);
                sqldb_query = sqldb_query = "CREATE TABLE IF NOT EXISTS MyTable(_id INTEGER PRIMARY KEY AUTOINCREMENT,date DATETIME DEFAULT CURRENT_TIMESTAMP, Value INTEGER);";
                sqldb.ExecSQL(sqldb_query);
                sqldb_query = "CREATE TABLE IF NOT EXISTS MyMonth(_id INTEGER PRIMARY KEY AUTOINCREMENT, Month VARCHAR, per INTEGER);";
                sqldb.ExecSQL(sqldb_query);
            }
            else
            {
                sqldb = SQLiteDatabase.OpenDatabase(sqldb_path, null, DatabaseOpenFlags.OpenReadwrite);
                sqldb_message = "Database: " + sqldb_name + " opened";
            }
            sqldb_available=true;
        }
        catch(SQLiteException ex) 
        {
            sqldb_message = ex.Message;
        }
    }
    //Adds a new record with the given parameters
    public void AddRecord(int svalue)
    {

        try
        {

            sqldb_query = "INSERT INTO MyTable(date, Value) VALUES (date() ," + svalue + ");";
            sqldb.ExecSQL(sqldb_query); 
            sqldb_message = "Record saved";
        }
        catch(SQLiteException ex) 
        {
            sqldb_message = ex.Message;
        }
    }

    public void AddRecordPer()
    {
        try
        {


            sqldb_query="INSERT INTO MyMonth(_id,Month,Per) VALUES(1,JAN,10);";

            sqldb.ExecSQL(sqldb_query);
            sqldb_message = "Record saved";
        }
        catch(SQLiteException ex) 
        {
            sqldb_message = ex.Message;
        }
    }

    public Android.Database.ICursor GetRecordCursor()
    {
        Android.Database.ICursor sqldb_cursor = null;
        try
        {
            sqldb_cursor = sqldb.RawQuery("SELECT * FROM MyTable ", null);
            if(!(sqldb_cursor != null))
            {
                sqldb_message =`enter code here` "Record not found";
            }
        }
        catch(SQLiteException ex) 
        {
            sqldb_message = ex.Message;
        }
        return sqldb_cursor;
    }



    public Android.Database.ICursor GetRecordCursor1()
    {
        Android.Database.ICursor sqldb_cursor = null;
        try
        {

            sqldb_cursor = sqldb.RawQuery("SELECT * FROM MyMonth", null);
            if(!(sqldb_cursor != null))
            {
                sqldb_message = "Record not found";
            }
        }
        catch(SQLiteException ex) 
        {
            sqldb_message = ex.Message;
        }
        return sqldb_cursor;
    }

}

}

0 个答案:

没有答案