数据库Sqlite的操作

it2026-02-15  18

​1.创建数据库   extends SQLiteOpenHelper  需要继承SQLiteOpenHelper

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.example.usersqllite.domain;    import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper;    public class UserSqlLiteHelp extends SQLiteOpenHelper {         public UserSqlLiteHelp(Context context) {          super (context, "User.db" , null , 1 );      }         // 创建数据库 初始化表结构      @Override      public void onCreate(SQLiteDatabase db) {             db.execSQL( "create table user ( id integer primary key autoincrement , name  varchar(20), age INTEGER  )" );      }         @Override      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {         }    }

2.数据的dao 增删改查 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 package com.example.usersqllite.dao;    import java.util.ArrayList; import java.util.List;    import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;    import com.example.usersqllite.domain.User; import com.example.usersqllite.domain.UserSqlLiteHelp;    public class UserDao {         private UserSqlLiteHelp help;         public UserDao(Context context) {          help = new UserSqlLiteHelp(context);      }         /**       * 添加数据       *        * @throws Exception       */      public void insert() throws Exception {          SQLiteDatabase sb = help.getWritableDatabase();          ContentValues values = new ContentValues();             for ( int i = 0 ; i < 30 ; i++) {              values.put( "name" , "aaa" + i);              values.put( "age" , 10 + i);              sb.insert( "User" , null , values);          }          sb.close();         }         /**       * 删除数据       * @return        *        * @throws Exception       */      public boolean delete( int id) throws Exception {          SQLiteDatabase sb = help.getWritableDatabase();          int result = sb.delete( "user" , "id=?" , new String[] { id + "" });          sb.close();          if (result> 0 )          {              return true ;          } else          {              return false ;          }                    }         /**       * 修改数据       *        * @throws Exception       */      public boolean update( int id, String data) throws Exception {          SQLiteDatabase sb = help.getWritableDatabase();          ContentValues values = new ContentValues();             values.put( "name" , data);          int result = sb.update( "user" , values, "id=?" , new String[] { id + "" });             sb.close();          if (result> 0 )          {              return true ;          } else          {              return false ;          }                 }         /**       * 查询数据       *        * @return       *        * @throws Exception       */      public List<User> findAll() throws Exception {             SQLiteDatabase sb = help.getWritableDatabase();          /**           * String table, String[] columns, String selection, String[]           * selectionArgs, String groupBy, String having, String orderBy)           */             List<User> list = new ArrayList<User>();          Cursor cursor = sb.query( "user" , new String[] { "id" , "name" , "age" },                  null , null , null , null , null , null );          while (cursor.moveToNext()) {              int id = cursor.getInt(cursor.getColumnIndex( "id" ));                 String name = cursor.getString(cursor.getColumnIndex( "name" ));                 int age = cursor.getInt(cursor.getColumnIndex( "age" ));                 User user = new User(id, name, age);                 list.add(user);              user = null ;             }          sb.close();          return list;         }    }

 

 

来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/feelbest/p/3696252.html

相关资源:数据结构—成绩单生成器
最新回复(0)