A. android怎么把数据存入数据库
把数据放入数据库
通过把ContentValues对象传入instert()方法把数据插入数据库:
// Gets the data repository in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, content);
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(
FeedReaderContract.FeedEntry.TABLE_NAME,
FeedReaderContract.FeedEntry.COLUMN_NAME_NULLABLE,
values);
insert()方法的第一个参数是表名。第二个参数提供了框架中的一个列名,在ContentValues的值是空的时候,框架会向表中插入NULL值(如果这个参数是“null”,那么当没有值时,框架不会向表中插入一行。
从数据库中读取数据
要从数据库中读取数据,就要使用query()方法,你需要给这个方法传入选择条件和你想要获取数据的列。查询结果会在Cursor对象中被返回。
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
FeedReaderContract.FeedEntry._ID,
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED,
...
};
// How you want the results sorted in the resulting Cursor
String sortOrder =
FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED + " DESC";
Cursor c = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
使用Cursor对象的移动方法来查看游标中的一行数据,在开始读取数据之前必须先调用这个方法。通常,应该从调用moveToFirst()方法开始,它会把读取数据的位置放到结果集中第一实体。对于每一行,你可以通过调用Cursor对象的相应的get方法来读取列的值,如果getString()或getLong()方法。对于每个get方法,你必须把你希望的列的索引位置传递给它,你可以通过调用getColumnIndex()或getColumnIndexOrThrow()方法来获取列的索引。例如:
cursor.moveToFirst();
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID)
);
从数据库中删除数据
要从一个表中删除行数据,你需要提供标识行的选择条件。数据API为创建选择条件提供了一种机制,它会防止SQL注入。这中机制把选择条件分成了选择条件和选择参数。条件子句定义了要查看的列,并且还允许你使用组合列来进行筛选。参数是用于跟条件绑定的、用户筛选数据的值。因为这样不会导致像SQL语句一样的处理,所以它避免了SQL注入。
// Define 'where' part of query.
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
// Specify arguments in placeholder order.
String[] selelectionArgs = { String.valueOf(rowId) };
// Issue SQL statement.
db.delete(table_name, selection, selectionArgs);
更新数据库
当你需要编辑数据库值的时候,请使用update()方法。
这个方法在更新数据时会把insert()方法中内容值的语法跟delete()方法中的where语法结合在一起。
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// New value for one column
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
// Which row to update, based on the ID
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
String[] selelectionArgs = { String.valueOf(rowId) };
int count = db.update(
FeedReaderDbHelper.FeedEntry.TABLE_NAME,
values,
selection,
selectionArgs);
B. android 用execSQL执行insert语句怎么知道是否插入成功
没有报错就是成功了。如果想确认可以从数据库中读取下你的插入记录。
C. android 怎么使用realm.insertorupdate
Intent(意图)主要是解决Android应用的各项组件之间的通讯。 为了实现传递数据这个目的需要以下步骤 Activity1需要构造一个 Intent,这个Intent用于告诉系统,我们要做“查看”动作 intent可调用putExtra来存放想要传递的数据 然后调用setClass,设置Activity1和欲调用的组件Activity2 最后调用startActivity将构造的Intent传入,系统会根据此Intent中的描述,到Activity1中找到满足此Intent要求的Activity,系统会调用找到的 Activity2最终传入Intent 在Activity2中可使用getIntent来获取传递的Intent,并通过获取数据的方法来获取数据 代码示例: Intent intent = new Intent(); // Activity1 intent.putExtra("one", num1); intent.putExtra("two", num2); intent.setClass(FirstActivity.this, SecondActivity.class); startActivity(intent); Intent intent = getIntent(); //Activity2 String num1 = intent.getStringExtra("one"); String num2 = intent.getStringExtra("two"); int ret = Integer.parseInt(num1) + Integer.parseInt(num2); result.setText(ret+"");注意:我们在使用intent的时候可以使用bundle传递复制的数据类型。
D. android 中SQLite如何插入数据后获得ID
String sql="insert into DishName (name_en,name_ch,price,isdiscount,Unit,PictureFile,Desc_ch) " + " values(???????)“ db.execSQL(sql); cur=db.rawQuery("select LAST_INSERT_ROWID() ",null); cur.moveToFirst();比较喜欢这种写法,如果字段一多db.insert总是返回-1,都不知道错哪里了
E. android 怎么往数据库里面添加数据
一、引入
数据库创建的问题解决了,接下来就该使用数据库实现应用程序功能的时候了。基
本的操作包括创建、读取、更新、删除,即我们通常说的 CRUD(Create, Read, Update, Delete)。
在实现这些操作的时候,我们会使用到两个比较重要的类 SQLiteDatabase 类和 Cursor 类。
二、创建表
1,execSQL(String sql):执行一条 sql 语句,且执行操作不能为 SELECT
因为它的返回值为 void,所以推荐使用 insert、update 方法等
2.,execSQL (String sql,Object[] bindArgs)
sql:执行一条 sql 语句
bindArgs:为 sql 语句中的?赋值
三、添加数据
1、execSQL(String sql)
2、使用对象的 insert 方法
ContentValues values = new ContentValues();
values.put(USERNAME, user.getUsername());
values.put(PASSWORD, user.getPassword());
db.insert(TABLE_NAME, null, values);
参数:
table:数据库中的表名
nullColumnHack:指定默认插入字段,为 null 时能插入数据
values:表示插入字段所对应的值,使用 put 方法。
四、删除数据
1、execSQL(String sql)
2、使用对象的 delete 方法
String whereClaues="_id=?";
String [] whereArgs={String.valueOf(id)};
//db.delete(TABLE_NAME, "_id="+id, null);
db.delete(TABLE_NAME, whereClaues, whereArgs);
参数
table:数据库的表名
whereClause:where 子句,比如:_id=?
whereArgs:where 子句中?的值
五、修改数据
1、execSQL(String sql)
2、使用对象的 delete 方法
ContentValues values = new ContentValues();
values.put(USERNAME, user.getUsername());
values.put(PASSWORD, user.getPassword());
String whereClaues="_id=?";
String [] whereArgs={String.valueOf(user.getId())};
db.update(TABLE_NAME, values, whereClaues, whereArgs);
参数
table:数据库的表名
values:代表要修改的值,修改方法还是 put(key,values)
whereClause:条件子句,比如 id=?,name=?
whereArgs:为 whereClause 中的?赋值,比如:new String[]{"1","张三"}
图:
参考代码:
程序内使用SQLite数据库是通过SQLiteOpenHelper进行操作
1.自己写个类继承SQLiteOpenHelper,重写以下3个方法
publicvoidonCreate(SQLiteDatabasedb)
{//创建数据库时的操作,如建表}
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion)
{
//版本更新的操作
}
2.通过SQLiteOpenHelper的getWritableDatabase()获得一个SQLiteDatabase数据库,以后的操作都是对SQLiteDatabase进行操作。
3.对得到的SQLiteDatabase对象进行增,改,删,查等操作。
代码
packagecx.myNote;
importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
//DBOptionsforlogin
publicclassDBOptions{
privatestaticfinalStringDB_NAME="notes.db";
privatestaticfinalStringDB_CREATE="createtablelogininf(nametext,pwdtext)";
{
publicDBHelper(Contextcontext){
super(context,DB_NAME,null,1);
}
@Override
publicvoidonCreate(SQLiteDatabasedb){
//TODOAuto-generatedmethodstub
//建表
db.execSQL(DB_CREATE);
}
@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
//TODOAuto-generatedmethodstub
db.execSQL("droptableifexistslogininf");
onCreate(db);
}
}
privateContextcontext;
privateSQLiteDatabasedb;
privateDBHelperdbHelper;
publicDBOptions(Contextcontext)
{
this.context=context;
dbHelper=newDBHelper(context);
db=dbHelper.getReadableDatabase();
}
//自己写的方法,对数据库进行操作
publicStringgetName()
{
Cursorcursor=db.rawQuery("selectnamefromlogininf",null);
cursor.moveToFirst();
returncursor.getString(0);
}
publicintchangePWD(StringoldP,Stringpwd)
{
ContentValuesvalues=newContentValues();
values.put("pwd",pwd);
returndb.update("logininf",values,"pwd="+oldP,null);
}
}
insert方法插入的一行记录使用ContentValus存放,ContentValues类似于Map,它提供了put(String key, Xxx value)(其中key为数据列的列名)方法用于存入数据、getAsXxxx(String key)方法用于取出数据
F. Android中线程与线程,进程与进程之间如何通信
使用handler发送message,消息队列排队
进程是一个具有独立功能的程序关于某个数据集合的一次运行活动。它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体。它不只是程序的代码,还包括当前的活动,通过程序计数器的值和处理寄存器的内容来表示。
进程是一个“执行中的程序”。程序是一个没有生命的实体,只有处理器赋予程序生命时,它才能成为一个活动的实体,我们称其为进程。
通常在一个进程中可以包含若干个线程,它们可以利用进程所拥有的资源。在引入线程的操作系统中,通常都是把进程作为分配资源的基本单位,而把线程作为独立运行和独立调度的基本单位。由于线程比进程更小,基本上不拥有系统资源,故对它的调度所付出的开销就会小得多,能更高效的提高系统内多个程序间并发执行的程度。
线程和进程的区别在于,子进程和父进程有不同的代码和数据空间,而多个线程则共享数据空间,每个线程有自己的执行堆栈和程序计数器为其执行上下文。多线程主要是为了节约CPU时间,发挥利用,根据具体情况而定。线程的运行中需要使用计算机的内存资源和CPU。
G. Android编程问题db.insert(table, nullColumnHack, values)
肯定没有问题啊,你每次插入相同的值也不会报错。 你应该去看一下数据库的相关东西,你每次执行这个语句都会新插入一行。
H. android开发中如何往调用的类库中的数据库插入数据
你通过getText()方法首先得到输入的值,然后调用数据库的插入方法 db.insert();插入到数据库中就行 就想这样
EditText et ;
String num = et.getText().toString();
public void addData(String num) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("num", num);
db.insert("表名", null, values);
}
当你调用这个 addData()方法时就会向数据库中插入数据了
I. Android数据库操作表的两种方法,通过sql 语句法和谷歌提供的api 方法进行比较情况如何
DirverManager类:是JDBC的管理层,作用于用户和驱动之间。该类负责注册和加载JDBC驱动。
Connection接口:代表与数据库的链接,并拥有创建SQL语句的方法,以完成基本的SQL操作,同时为数据库事务提供提交和回滚方法。如:上面的例子就是链接到了TestData数据库。
Statement接口:用于执行不带参数的简单SQL语句。创建Statement实例对象后可以调用JDBC提供的3种执行SQL语句的方法:
(1)executeUpdate()方法,一般用于执行SQL的INSERT,DELETE,UPDATE语句
(2)executeQuery()方法,一般用于执行SQL的SELECT语句,因为 它的返回值是执行SQL语句后产生的一个ResultSet接口的实例(结果集)
(3)execute()方法,即一般它执行的SQL语句既有查询又有更新值,约等于executeUpdate()和executeQuery()两个方法的合辑。
PreparedStatement接口:它与Statement 的主要区别
(1)它包含的SQL语句是预编译的,所以当多次执行一条SQL语句时用它会更快
(2)在设置参数是可以用“?”代替。如:
PreparedStatement pstmt=conn.preparedStatement(insert into test values(?,?));
pstmt.setString(1,'gg');
pstmt.setString(2,'123');
ResultSet接口:包含了Statement和PreparedStatement的executeQuery方法中SELECT的结果集。相当于用它来读取数据库里每列的值。
DatabaseMetaData接口:主要是用来得到数据库的相关信息的。如:数据库版本啊
ResultSetMetaData接口:主要是用来获取数据库中表的相关信息的。如:表的行数啊。,谢谢
J. Android中 这句语言什么意思啊:private static final int INSERT_ID = Menu.FIRST+1
定义一个常量整型”INSERT_ID“,值为menu.first + 1;其中Menu.FIRST在reference中描述为:First value for group and item identifier integers.我们可以理解为ID设置的最小数值。当然即使用其他的常量替代Menu.FIRST也不会影响实际的结果,主要是这是程序封装的变量,用起来不占内存,程序读的快,不容易出错