如何添加数据到我的SQLite数据库? [英] How do I add data into my SQLite Database?

查看:90
本文介绍了如何添加数据到我的SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充说,在胜场列数500,我该怎么办呢?我只想补充一点,一个数据内。另外,你知道如何使用文本视图然后我就可以显示在我的主要活动?

 公共类数据库{

公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_NAME =number_of_wins;

私有静态最后弦乐DATABASE_NAME =高分;
私有静态最后弦乐DATABASE_TABLE =Total_Wins;
私有静态最终诠释DATABASE_VERSION = 1;

私人DBHelper ourHelper;
私人最终语境ourContext;
私人SQLiteDatabase ourDatabase;

私有静态类DBHelper扩展SQLiteOpenHelper {

    公共DBHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        // TODO自动生成构造函数存根
    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        db.execSQL(CREATE TABLE+ DATABASE_TABLE +(+
                KEY_ROWID +INTEGER PRIMARY KEY AUTOINCREMENT,+
                KEY_NAME +INTEGER);
                );
    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        db.execSQL(DROP TABLE IF EXISTS+ DATABASE_TABLE);
        的onCreate(DB);
    }

}
公共数据库(上下文的背景下){
    ourContext =背景;
}

公共数据库的open(){
    ourHelper =新DBHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    回到这一点;
}

公共无效的close(){
    ourHelper.close();
}
  }
 

解决方案

请看一看的 Vogella的在Android上的SQLite 的教程。你需要的SQL查询的一个基本的了解,他们是如何工作的,所以我建议你看看之前你跳进Android系统。

如果你想快速练习你的SQL技能,使用 SQLZoo 。他们不仅教一步一步的,他们还为您提供设施,以在其网站上执行查询。

I want to add say 500 in the number of wins column, how do I do that? I just want to add that one piece of data inside. Also do you know how I can then display that into my main activity using a text view?

public class Database {

public static final String KEY_ROWID = "_id"; 
public static final String KEY_NAME = "number_of_wins"; 

private static final String DATABASE_NAME = "HIGHSCORES"; 
private static final String DATABASE_TABLE= "Total_Wins"; 
private static final int DATABASE_VERSION = 1;

private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE" + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " INTEGER);"
                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}
public Database(Context context) {
    ourContext = context;
}

public Database open() {
    ourHelper = new DBHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}
  }

解决方案

Please have a look at Vogella's Tutorial on Android SQLite. You need a basic understanding of SQL queries and how they work so I suggest you look into that before you jump into Android.

If you want to quickly practice your SQL skills, use SQLZoo. Not only do they teach step-by-step, they also provide you with facilities to execute queries on their website.

这篇关于如何添加数据到我的SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆