现在在if语句里用输入的字符串URL与SQLite里保存URL表里的FILEURL列里的字符串做判断,if里的语句怎么写呢?在if里写SQL查询语句吗?求给个代码
安卓应该有equals 方法吧!!!
url.equals(fileurl);
没有的话 那就算了 2333
你先用SQl查询语句查出相应字段下的值,然后用这个值与URL去做判断啊
package com.example.naruto;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private int total = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et_fileurl = (EditText) findViewById(R.id.fileurl);
final Button btn_down = (Button) findViewById(R.id.btn_down);
btn_down.setOnClickListener(new View.OnClickListener(){ //xiang ying shi jian
public void onClick(View v){
new Thread(new Runnable(){
@Override
public void run() {
try {
URL url =new URL(et_fileurl.getText().toString()); //资源
// ("select * from url where fileurl=?");
Connection conn1 = null;
PreparedStatement ps = null;
conn1=DriverManager.getConnection(url);
ps = conn1.prepareStatement("select * from url where fileurl=?");
if(et_fileurl.equals(ps)){
Toast.makeText(MainActivity.this, "手机里有该文件",Toast.LENGTH_SHORT).show();
return;
}
//
// if(url=select * from url where fileurl=?)){
//
// }
// public class url{
// private String fileurl;
// public string getFileurl(){
// return fileurl;
// }
// public void setFileurl(String fileurl){
// this.fileurl = fileurl;
// }
// }
//
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compayible; MSIE8.0; Windows NT 5.1; Trient/4.0; .NET CLR 2.0.50727)");//伪装成浏览器
String str = conn.getResponseMessage();
int length = conn.getContentLength();//huo qu zheng ge wen jian de chang du
if(length <0 ){ //pan duan wen jian cun bu cun zai
Toast.makeText(MainActivity.this, "文件不存在",Toast.LENGTH_SHORT).show();
return;
}
// Connection conn1 = null;
// PreparedStatement ps = null;
// ps = conn1.prepareStatement("select * from url where fileurl=?");
// if(et_fileurl.equals(ps)){
// Toast.makeText(MainActivity.this, "手机里有该文件",Toast.LENGTH_SHORT).show();
// return;
// }
File file = new File(Environment.getExternalStorageDirectory(),getFileName(et_fileurl.getText().toString()));//huo qu wen jian ming
RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
randomFile.setLength(length);
int blocksize = length/3;
for(int i=0;i<3;i++){
int begin = i*blocksize;
int end = (i+1)*blocksize;
if(i==2){
end=length;
}
//chuang jian xin de xian cheng, xia zai wen jian
Thread t =new Thread(new DownloadRunnable(i, begin, end, file, url));
t.start();
}
}catch (MalformedURLException e){
Toast.makeText(MainActivity.this, "URL不正确",Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private String getFileName(String url){
int index = url.lastIndexOf("/");
return url.substring(index);
}
class DownloadRunnable implements Runnable{
private int begin;
private int end;
private File file;
private URL url;
private int id;
public DownloadRunnable(int id, int begin, int end, File file, URL url){
this.file=file;
this.id=id;
this.begin=begin;
this.end=end;
this.url=url;
}
@Override
public void run() {
// xia zai xing wei de bian xie
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compayible; MSIE8.0; Windows NT 5.1; Trient/4.0; .NET CLR 2.0.50727)");
conn.setRequestProperty("Range", "bytes=" + begin + "-" + end);
InputStream is = conn.getInputStream();
byte[] buf = new byte[1024 * 1024];
RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
randomFile.seek(begin);
int len = 0;
while ((len = is.read(buf)) != -1 ){
randomFile.write(buf,0,len);
updateProgress(len); // dui jin du geng xin
Log.d("Download length: ","" + total);
}
is.close();
randomFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
synchronized private void updateProgress(int add){
total += add;
}
}
package com.example.naruto;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SQLiteReadActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView fileurlTV = (TextView) findViewById(R.id.fileurl);
DBHelper helper = new DBHelper(SQLiteReadActivity.this);
UR ur = helper.query(1);
fileurlTV.setText("file:"+ur.getFileurl());
}
}
package com.example.naruto;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SQLiteWriteActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText fileurlET = (EditText) findViewById(R.id.fileurl);
Button btn_down = (Button) findViewById(R.id.btn_down);
btn_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fileurl =fileurlET.getText().toString();
UR ur = new UR(fileurl);
DBHelper helper = new DBHelper(SQLiteWriteActivity.this);
helper.insert(ur);
Intent intent = new Intent();
//tiao zhuan dao
intent.setClass(SQLiteWriteActivity.this, SQLiteReadActivity.class);
startActivity(intent);
}
});
}
}
package com.example.naruto;
public class UR {
private int id;
private String fileurl;
public UR(){}
public UR(String fileurl){
this.fileurl = fileurl;
}
public int getID(){
return id;
}
public String getFileurl(){
return fileurl;
}
public void setFileurl(String fileurl){
this.fileurl = fileurl;
}
}
<?xml version="1.0" encoding="utf-8"?>
package="com.example.naruto"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.naruto.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
你的需求:判断数据库表中是否存在某个URL,你完全可以写 这样一个语句:
select count(1) from table_name where file_url = 'your url';
如果存在返回值大于0,如果不存在返回值等于0;
这时再来if判断。