您的位置:首页 > 数据库

SQL数据库建立

2015-11-05 21:43 344 查看

      SQLiteOpenHelper数据库

   

    第一做一个继承SQLiteOpenHelper


    

<span style="font-size:18px;">public class MyDBhelp extends SQLiteOperHelper{
</span>
<span style="font-size:18px;">private final static String SQLName="student";//给数据库取个名字</span>
<span style="font-size:18px;">private final static int VERSION=1;//设置数据库的版本</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">//构造类的构造方法,注意这是必须要写的,如果不写就会报编译事情异常;</span>
<span style="font-size:18px;">public MyDBhelp(Context context,){</span>
<span style="font-size:18px;">      //第一个参数是调用它的Activity</span>
<span style="font-size:18px;">      //第二个参数是数据库的名字</span>
<span style="font-size:18px;">      //第三个参数可以填null</span>
<span style="font-size:18px;">      //第四个参数是数据库的版本号</span>
<span style="font-size:18px;">super(content,SQLName,null,VERSION);</span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">//重写SQLiteOperHelper的两个方法</span>
<span style="font-size:18px;">@Override</span>
<span style="font-size:18px;">public void onCreate(SQLiteOpenHelper db){</span>
<span style="font-size:18px;">       //固定格式“CREATE TABLE 表名(列名 列的类型,</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">列名 列的类型,</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">列名 列的类型</span><span style="font-size:18px;">)”</span>
<span style="font-size:18px;">      String sql="CREATE TABLE AndroidMessager(id INETEGER,name TXET,age INETEGER);";</span>
<span style="font-size:18px;">      </span>
<span style="font-size:18px;">      //把格式放进数据库</span>
<span style="font-size:18px;">      db.execSQL(sql);</span>
<span style="font-size:18px;">       </span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">@Override//这个方法是版本升级的方法</span>
<span style="font-size:18px;">public void onUpgrade(SQLiteOpenHelper db,int oldVersion,int newVersion){</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">}</span>

  第二做一个数据库的工具

<span style="font-size:18px;">public class StudentTool{
</span>
<span style="font-size:18px;">MyDBhelp mhelp;//定义数据库帮助类</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">//构造构造方法</span>
<span style="font-size:18px;">public StudentTool(Context context){</span>
<span style="font-size:18px;">mhelp=new MyDBhelp(context);</span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">//创建数据库插入方法</span>
<span style="font-size:18px;">//@param -stu要存储的数据对象</span>
<span style="font-size:18px;">public void insert(Student stu){</span>
<span style="font-size:18px;">//<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">  SQLiteDatabase类</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">用于</span><span class="t_tag" href="tag.php?name=%B9%DC%C0%ED" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">管理</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">和操作SQLite数据库,几乎所有的数据库操作,最终都将由这个类完成。</span></span>
<span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">//获得SQLite数据库类</span></span>
<span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">SQLiteDatabase database=mhelp.getWritableDatabase();</span></span>
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><span style="font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">//<span style="color: rgb(51, 51, 51); font-family: arial; font-size: 13px; line-height: 20.0200004577637px;">负责存储一些名值对,但是它存储的名值对当中的名是一个String类型,而值都是基本类型。</span></span></span>
<span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">ContentValues values=new  ContentValues();</span></span>
<span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">
</span></span>
<span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">//添加数据</span></span>
<span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">values.put("id",stu.getId());</span></span>
<span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);"></span></span><pre name="code" class="html"><span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">values.put("name",stu.getName());</span></span>
<span style="font-size:18px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">values.put("age",stu.getAge());</span></span>


<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><span style="font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">//添加数据到数据库</span></span>
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><span style="font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">//第一个参数是数据库名</span></span>
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><span style="font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">//第二个才是可以是null</span></span>
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><span style="font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">//第三个参数是要插入的参数</span></span>
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><span style="font-size: 14.3999996185303px; background-color: rgb(238, 238, 221);">database.insert("AndroidMassager",null,values);</span></span>

<span style="font-size:18px;">
</span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">  第三做个实例类</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">public class Student{</span>
<span style="font-size:18px;">      </span>
<span style="font-size:18px;">     private int id;</span>
<span style="font-size:18px;">private String name;</span>
<span style="font-size:18px;">private int age;</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">public Student(){</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">public Student(int id,String name,int age){</span>
<span style="font-size:18px;"> this.id=id;</span>
<span style="font-size:18px;">this.name=name;</span>
<span style="font-size:18px;">this.age=age;   </span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">此处省略</span>
<span style="font-size:18px;">set;;;;;;</span>
<span style="font-size:18px;">get;;;;;</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">  第四 在Activity中调用数据库</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">public class MySQL_Activity extends Activity{</span>
<span style="font-size:18px;">StudentTool stl;</span>
<span style="font-size:18px;">Student stu;</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">public void onCreate(Bundle savedInstanceState){</span>
<span style="font-size:18px;">super(savedInstanceState);</span>
<span style="font-size:18px;">setContentView(R.layout.activity_myaql);</span>
<span style="font-size:18px;">stl=new StudentTool(this);</span>
<span style="font-size:18px;">stu=new Student();</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">Button btnOver=(Button)findViewById(R.id.btn_over);</span>
<span style="font-size:18px;">btnOver.setOnClickListener(inputMassager);</span>
<span style="font-size:18px;">}</span>
<span style="font-size:18px;">OnClickListener inputMassager=new OnClickListener(){</span>
<span style="font-size:18px;">@Override</span>
<span style="font-size:18px;">public void OnClick(){</span>
<span style="font-size:18px;">EidtView etv_id=(EidtView)findViewById(R.id.etv_id);</span>
<pre name="code" class="html" style="font-size:18px;"><span style="font-size:18px;">EidtView etv_name=(EidtView)findViewById(R.id.etv_name);</span>
<span style="font-size:18px;">EidtView etv_age=(EidtView)findViewById(R.id.etv_age);</span>
<span style="font-size:18px;">//把数据封装到实体类中</span>
<span style="font-size: 18px;">stu.setId(Integer.parseInt(etv_id.getText.tostring().trim()));</span>
<span style="font-size: 18px;">stu.setname(etv_name.getText.tostring().trim());</span>
<span style="font-size: 18px;">stu.setage(Integer.parseInt(etv_age.getText.tostring().trim()));</span>
<span style="font-size: 18px;">
</span>
<span style="font-size: 18px;">//把封装好数据的实体类作为参数传到数据库</span>
<span style="font-size: 18px;">stl.insert(stu);</span>


<span style="font-size:18px;">
</span>
<span style="font-size:18px;">}</span>

<span style="font-size:18px;">}</span>
<span style="font-size:18px;">}</span>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: