Java教程

SQL数据库

本文主要是介绍SQL数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.实现通讯录的功能

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#E2EDF5"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et_id"
        android:layout_width="280dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"/>
    <EditText
        android:id="@+id/et_name"
        android:layout_width="280dp"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/et_price"
        android:layout_width="280dp"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加"
            android:textColor="#000000"
            android:textSize="18sp"
            android:layout_marginTop="30dp"
            android:onClick="add"/>
        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"
            android:textColor="#000000"
            android:textSize="18sp"
            android:layout_marginTop="30dp"
           android:onClick="delete" />
        <Button
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改"
            android:textColor="#000000"
            android:textSize="18sp"
            android:layout_marginTop="30dp"
            android:onClick="update"/>
        <Button
            android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询"
            android:textColor="#000000"
            android:textSize="18sp"
            android:layout_marginTop="30dp"
            android:onClick="search"/>
    </LinearLayout>
   <LinearLayout
       android:id="@+id/ll_title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

       <TextView
           android:layout_width="60dp"
           android:layout_height="wrap_content"
           android:text="编号"
           android:textColor="#000000"
           android:textSize="18sp"/>
       <TextView
           android:layout_width="120dp"
           android:layout_height="wrap_content"
           android:text="名称"
           android:textColor="#000000"
           android:textSize="18sp"/>
       <TextView
           android:layout_width="120dp"
           android:layout_height="wrap_content"
           android:text="价格"
           android:textColor="#000000"
           android:textSize="18sp"/>

   </LinearLayout>
    <ListView
        android:id="@+id/lv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout> 
list_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="60dp"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="120dp"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv_price"
        android:layout_width="120dp"
        android:layout_height="wrap_content"/>


</LinearLayout>
package com.example.sql;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout title=findViewById(R.id.ll_title);
        title.setVisibility(View.INVISIBLE);
    }
    public void add(View view){
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String name =((EditText)findViewById(R.id.et_name)).getText().toString();
        String price =((EditText)findViewById(R.id.et_price)).getText().toString();
        db.execSQL("insert into information(name,price) values(?,?)",new Object[]{name,price});
        db.close();
        Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show();
    }
    public void delete(View view){
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String id = ((EditText)findViewById(R.id.et_id)).getText().toString();
        db.execSQL("delete from information where _id=?",new Object[]{id});
        db.close();
        Toast.makeText(this,"删除成功",Toast.LENGTH_LONG).show();
    }
    public void update(View view){
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String name =((EditText)findViewById(R.id.et_name)).getText().toString();
        String price =((EditText)findViewById(R.id.et_price)).getText().toString();
        String id = ((EditText)findViewById(R.id.et_id)).getText().toString();
        db.execSQL("update information set name=?,price=? where _id=?",new Object[]{name,price,id});
        db.close();
        Toast.makeText(this,"修改成功",Toast.LENGTH_LONG).show();
    }
    public void search(View view){
        LinearLayout title=findViewById(R.id.ll_title);
        title.setVisibility(View.VISIBLE);
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String name =((EditText)findViewById(R.id.et_name)).getText().toString();
        Cursor cursor =db.rawQuery("select * from information where name like ?",new String[]{"%"+name+"%"});
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.list_item,cursor,new String[]{"_id","name","price"}, new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_price});
        ListView lv =findViewById(R.id.lv_result);
        lv.setAdapter(adapter);
        db.close();
    }
}
package com.example.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MyHelper extends SQLiteOpenHelper {
    public MyHelper( Context context) {
        super(context,"stu.db", null,2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table information(_id integer primary key autoincrement,name,price)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       System.out.println("数据库升级了!!!");
    }
}

 

 

这篇关于SQL数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!